Given an array arr, the task is to find the elements of a contiguous subarray of numbers which has the largest sum.
Input: arr = [-2, -3, 4, -1, -2, 1, 5, -3]
Output: [4, -1, -2, 1, 5]
In the above input the maximum contiguous subarray sum is 7 and the elements of the subarray are [4, -1, -2, 1, 5]
Input: arr = [-2, -5, 6, -2, -3, 1, 5, -6]
Output: [6, -2, -3, 1, 5]
In the above input the maximum contiguous subarray sum is 7 and the elements
of the subarray are [6, -2, -3, 1, 5]
Naive Approach: The naive approach is to generate all the possible subarray and print that subarray which has maximum sum.
Time complexity: O(N2)
Auxiliary Space: O(1)
Efficient Approach: The idea is to use the Kadane’s Algorithm to find the maximum subarray sum and store the starting and ending index of the subarray having maximum sum and print the subarray from starting index to ending index. Below are the steps:
- Initialize 3 variables endIndex to 0, currMax and globalMax to first value of the input array.
- For each element in the array starting from index(say i) 1, update currMax to max(nums[i], nums[i] + currMax) and globalMax and endIndex to i only if currMax > globalMax.
- To find the start index, iterate from endIndex in the left direction and keep decrementing the value of globalMax until it becomes 0. The point at which it becomes 0 is the start index.
- Now print the subarray between [start, end].
Below is the implementation of the above approach:
6 -2 -3 1 5
Time complexity: O(N)
Auxiliary Space: O(1)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Maximum length of subarray such that all elements are equal in the subarray
- Maximum length of subarray such that sum of the subarray is even
- Queries to multiply the given subarray with given number X and print sum
- Maximum sum subarray having sum less than or equal to given sum using Set
- Longest subarray having maximum sum
- Maximum sum bitonic subarray
- Maximum Subarray Sum in a given Range
- Maximum subarray sum in O(n) using prefix sum
- Maximum Product Subarray | Set 3
- Size of The Subarray With Maximum Sum
- Maximum sum subarray of even length
- Maximum sum subarray having sum less than or equal to given sum
- Maximum Product Subarray
- Maximum circular subarray sum
- Maximum Subarray Sum after inverting at most two elements
- Maximum sum subarray such that start and end values are same
- Maximum Sum SubArray using Divide and Conquer | Set 2
- Length of maximum product subarray
- Maximum Product Subarray | Set 2 (Using Two Traversals)
- Maximum sum subarray removing at most one element
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.