You are given an array (ARR) of length N, consisting of integers. You have to find the sum of the subarray (including empty subarray) having maximum sum among all subarrays.
A subarray is a contiguous segment of an array. In other words, a subarray can be formed by removing 0 or more integers from the beginning, and 0 or more integers from the end of an array.
Note :
The sum of an empty subarray is 0.
Input Format :
The first line of input contains an integer N, representing the length of the array.
The second line of input contains N single space-separated integers, denoting the elements of the array.
Output Format :
In the only output line, output the maximum subarray sum.
Note :
You are not required to print the output explicitly, it has already been taken care of. Just implement the function.
Constraints :
1 <= N <= 10^6
-10^6 <= A[i] <= 10^6
where N is the length of the array.
A[i] represents the numbers present in the array.
Time limit: 1sec
Looking at the previous approach,we realize that to compute curSum[i], we only require the value of curSum[i-1]. Hence, instead of maintaining a whole array, we can simply keep track o...read more
I explained to him my approach (Kadane’s algorithm) and he seems satisfied with it and asked to code it down.
Let us check for all possible subarrays of the array. For this, we run two loops, where the outer loop points to the left boundary and the inner loop points to the outer boundary o...read more
According to the previous approach, let us fix the left boundary of the subarray in the outer loop. In the inner loop, we move our right boundary by one unit, every time.
Let’s sa...read more
- Divide the array into 2 halves.
- Get the answer of left and right parts of the array by recursion.
- Get the answer of Maximum subarray sum such that the subarray crosses the mid...read more
Let us start from the left of the array. We maintain an array curSum[], which keeps track of the maximum sum of the subarray ending at the index we’re at.
While traversing the array,...read more
Top Tata 1mg Software Developer interview questions & answers
Popular interview questions of Software Developer
Reviews
Interviews
Salaries
Users/Month