Last Updated: 26 Nov, 2020

Maximum subarray

Moderate
Asked in companies
AmazonFacebookOLX Group

Problem statement

Ninjas has been given an array. He wants to find a subarray such that the sum of all elements in the subarray is maximum.

Subarray 'A' is greater than sub-array 'B' if sum(A) > sum(B). If two sub-array have the same maximum sum, then output the subarray that has a larger length.

A subarray means a contiguous part of an array. For example, In 'arr' = [1, 2, 3, 4], [1, 2], [2, 3, 4] are the contiguous subarry but [1, 3, 4] is not a subarray.

Note:

More than one sub-array can have a maximum sum, in that case, output any.
Input Format:
The first line contains an integer 'T' which denotes the number of test cases or queries to be run.

The first line of each test case contains a single integer ‘N’ denoting the size of the array.

The second line of each test case contains ‘N’ space-separated integers denoting the elements of the array.
Output Format:
For each case, If the returned subarray is correct then print 1, else print 0.

The output of each test case will be printed in a separate line.
Note:
You do not need to input or print anything, and it has already been taken care of. Just implement the given function.
Constraints:
1 <= T <= 5
1 <= N <= 1000
-99 <= |arr| <= 99

Time limit: 1 sec.

Approaches

01 Approach

Our Brute force approach will figure out all the possible combinations possible. To find a subarray, we need to know both the starting point and the ending out of the array. We can make a loop that would iterate through the array. During each iteration, we will make another nested loop inside which will take all possible combinations where the starting index will be the pointer of the parent loop and the endpoint will be the pointer of the current loop. We can take a few variables which would be updated whenever we get a new subarray with a larger sum.

 

Algorithm:

 

  • Declare a variable ‘maxSum’ and initialize it as ‘minimum integer’
  • Declare and initialize two variables as ‘start’ and ‘end’ and initialize them as ‘0’ and ‘-1’ respectively.
  • Run a loop ‘i’ = 0 to ‘N’ traversing all the elements
    • Declare a local variable as ‘localSum’ and initialize it as ‘0’
    • Run a loop ‘j’ = ‘i’ to N
      • Add the current element to the ‘localSum’
      • If the ‘localSum’ is greater than the ‘maxSum’
        • ‘maxSum’ = ‘localSum’
        • ‘start’ = ‘i’
        • ‘end’  = ‘j
      • If the 'localSum' is equal to the ‘maxSum’ and the difference between ‘end’ and ‘start’ is less than the difference between ‘j’ and ‘i’
        • ‘start’ = ‘i’
        • ‘end’ = ‘j’
  • Return the part of the array starting from ‘start’ and ending at ‘end’.

 

02 Approach

As we know that we cannot have a negative-sum, we can simply maintain two variables as maximum and local sum where we would add every element to local sum. If our local sum exceeds our maximum sum, we shall update our maximum sum and even note down the index. If in any case, our local sum becomes negative, then it’s better to not consider any elements only and we would again start from the next index with local sum as 0.

 

Algorithm:

 

  • Declare a variable ‘maxSum’ and initialize it with ‘minimum integer’
  • Declare a variable ‘localSum’ and initialize it with ‘0’
  • Declare 3 counter variables as ‘start’ , ‘end’ , ‘newStart’ as 0, 0, 0
  • Run a loop from i = 0 to N
    • Add a current element to ‘localSum’
    • If 'localSum' is greater than 'maxSum'
      • Update ‘maxSum’ with 'localSum', update start with ‘newStart’ and end with loop counter ‘i’
    • If 'localSum' is equal to ‘maxSum’ and the difference between ‘end’ and ‘start’ is less than the difference between ‘newStart’ and ‘i’
      • Update start with ‘newStart’ and end with ‘i’
    • If 'localSum' is below ‘0’
      • Update ‘localSum’ as 0 and set ‘newStart' as ‘i’ + 1
  • Return the part of the array starting from ‘start’ and ending at ‘end’