An array is given, find length of the subarray having maximum sum.
Examples :
Input : a[] = {1, -2, 1, 1, -2, 1} Output : Length of the subarray is 2 Explanation: Subarray with consecutive elements and maximum sum will be {1, 1}. So length is 2 Input : ar[] = { -2, -3, 4, -1, -2, 1, 5, -3 } Output : Length of the subarray is 5 Explanation: Subarray with consecutive elements and maximum sum will be {4, -1, -2, 1, 5}.
This problem is mainly a variation of Largest Sum Contiguous Subarray Problem.
The idea is to update starting index whenever sum ending here becomes less than 0.
Python3
# Python3 program to print largest contiguous array sum from sys import maxsize # Function to find the maximum contiguous subarray # and print its starting and end index def maxSubArraySum(a,size): max_so_far = - maxsize - 1 max_ending_here = 0 start = 0 end = 0 s = 0 for i in range ( 0 ,size): max_ending_here + = a[i] if max_so_far < max_ending_here: max_so_far = max_ending_here start = s end = i if max_ending_here < 0 : max_ending_here = 0 s = i + 1 return (end - start + 1 ) # Driver program to test maxSubArraySum a = [ - 2 , - 3 , 4 , - 1 , - 2 , 1 , 5 , - 3 ] print (maxSubArraySum(a, len (a))) |
5
Time Complexity: O(N) where N is the size of the input array. This is because a for loop is executed from 1 to the size of the array.
Auxiliary Space: O(1) as no extra space has been taken.
Approach#2: Using Kadane’s algorithm
This approach implements Kadane’s algorithm to find the maximum subarray sum and returns the size of the subarray with the maximum sum.
Algorithm:
- Initialize max_sum, current_sum, start, end, max_start, and max_end to the first element of the array.
- Iterate through the array from the second element.
- If the current element is greater than the sum of the current element and current_sum, update start to the current index.
- Update current_sum as the maximum of the current element and the sum of current element and current_sum.
- If current_sum is greater than max_sum, update max_sum, end to the current index, and max_start and max_end to start and end respectively.
- Return max_end – max_start + 1 as the size of the subarray with maximum sum.
Below is the implementation of the approach:
Python3
# Python3 implementation of the approach # Function to find the maximum subarray sum def max_subarray_sum(a): n = len (a) # Initializing initial and final sum to 0 max_sum = a[ 0 ] current_sum = a[ 0 ] start = 0 end = 0 max_start = 0 max_end = 0 # Traverse the array for i in range ( 1 , n): # If the current element is greater than the sum so # far plus the current element, then update the # start index to the current index if a[i] > current_sum + a[i]: start = i # Update the current sum to be either the current # element or the sum so far plus the current # element current_sum = max (a[i], current_sum + a[i]) # If the current sum is greater than the maximum # sum so far, then update the maximum sum and its # start and end indices if current_sum > max_sum: max_sum = current_sum end = i max_start = start max_end = end # Return the length of the maximum subarray return max_end - max_start + 1 # Initializing array if __name__ = = "__main__" : a = [ - 2 , - 3 , 4 , - 1 , - 2 , 1 , 5 , - 3 ] # Function call print (max_subarray_sum(a)) |
5
Time Complexity: O(n), where n is length of array
Auxiliary Space: O(1)
Note: The above code assumes that there is at least one positive element in the array. If all the elements are negative, the code needs to be modified to return the maximum element in the array.
Please refer complete article on Size of The Subarray With Maximum Sum for more details!
You’ll access excellent video content by our CEO, Sandeep Jain, tackle common interview questions, and engage in real-time coding contests covering various DSA topics. We’re here to prepare you thoroughly for online assessments and interviews.
Ready to dive in? Explore our free demo content and join our DSA course, trusted by over 100,000 neveropen! Whether it’s DSA in C++, Java, Python, or JavaScript we’ve got you covered. Let’s embark on this exciting journey together!