Last Updated: 3 Aug, 2020

Maximum Subarray Sum

Moderate
Asked in companies
QualcommUberAmazon

Problem statement

You are given an array 'arr' of length 'n', consisting of integers.


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.


Find the sum of the subarray (including empty subarray) having maximum sum among all subarrays.


The sum of an empty subarray is 0.


Example :
Input: 'arr' = [1, 2, 7, -4, 3, 2, -10, 9, 1]

Output: 11

Explanation: The subarray yielding the maximum sum is [1, 2, 7, -4, 3, 2].
Input Format :
The first line of input contains an integer 'n', representing the array's length.

The second line of input contains 'n' single space-separated integers, denoting the elements of the array.


Output Format :
The output contains the maximum subarray sum.


Note :
You do not need to print anything; it has already been taken care of. Just implement the given function.

Approaches

01 Approach

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 of the subarray. 

Using another loop inside, find the sum of this subarray. Compare it with the maximum subarray sum obtained so far. After checking all the subarrays, simply return the maximum sum found.

02 Approach

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 of the previous value only.

03 Approach

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, If the value of curSum[i] becomes negative, we can simply discard this part of the array because it has a negative contribution to the subarray sum. It would be better to just drop that part and have a zero contribution rather than a negative one. Then, we can start anew with the next element. In other words, curSum[i] can be reset to 0.

The maximum value in the curSum array will be our answer.

04 Approach

  1. Divide the array into 2 halves.
  2. Get the answer of left and right parts of the array by recursion.
  3. Get the answer of Maximum subarray sum such that the subarray crosses the midpoint.
  4. To find the maximum subarray sum of crossing points we need to find the maximum sum starting from mid point and ending at some point on the left of mid, then find the maximum sum starting from mid + 1 and ending with some point on right of mid + 1. Total sum would be the sum of both left part and right part.
  5. Maximum of all three sums would be the final answer.

05 Approach

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 say for a particular subarray, we have computed its sum. Now when we move the right boundary by one unit, instead of recomputing the sum for the new subarray all over again, we can simply add the value of the new element to the sum of the previous subarray.

In this way, we can reduce our time complexity by remembering the previous result.