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.
- First subarray having sum at least half the maximum sum of any subarray of size K
- Maximum length of subarray such that sum of the subarray is even
- Maximum sum subarray having sum less than or equal to given sum
- Maximum sum subarray having sum less than or equal to given sum using Set
- Maximum length of subarray such that all elements are equal in the subarray
- Maximize the subarray sum after multiplying all elements of any subarray with X
- Count of subarray that does not contain any subarray with sum 0
- Maximum subarray sum in O(n) using prefix sum
- Queries to multiply the given subarray with given number X and print sum
- Maximum circular subarray sum
- Maximum Subarray Sum using Divide and Conquer algorithm
- Find Maximum Sum Strictly Increasing Subarray
- Maximum sum subarray removing at most one element
- Find maximum (or minimum) sum of a subarray of size k
- Maximum sum bitonic subarray
- Maximum subarray sum in an array created after repeated concatenation
- Maximum Subarray Sum in a given Range
- Size of The Subarray With Maximum Sum
- Maximum Subarray Sum Excluding Certain Elements
- Split array to three subarrays such that sum of first and third subarray is equal and maximum
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.