Given an array arr[] containing n integers. The problem is to find the length of the subarray having maximum sum. If there exists two or more subarrays with maximum sum then print the length of the longest subarray.
Examples:
Input : arr[] = {5, -2, -1, 3, -4}
Output : 4
There are two subarrays with maximum sum:
First is {5}
Second is {5, -2, -1, 3}
Therefore longest one is of length 4.
Input : arr[] = {-2, -3, 4, -1, -2, 1, 5, -3}
Output : 5
The subarray is {4, -1, -2, 1, 5}
Approach: Following are the steps:
- Find the maximum sum contiguous subarray. Let this sum be maxSum.
- Find the length of the longest subarray having sum equal to maxSum. Refer this post.
C++
// C++ implementation to find the length of the longest // subarray having maximum sum #include <bits/stdc++.h> using namespace std; // function to find the maximum sum that // exists in a subarray int maxSubArraySum(int arr[], int size) { int max_so_far = arr[0]; int curr_max = arr[0]; for (int i = 1; i < size; i++) { curr_max = max(arr[i], curr_max + arr[i]); max_so_far = max(max_so_far, curr_max); } return max_so_far; } // function to find the length of longest // subarray having sum k int lenOfLongSubarrWithGivenSum(int arr[], int n, int k) { // unordered_map 'um' implemented // as hash table unordered_map<int, int> um; int sum = 0, maxLen = 0; // traverse the given array for (int i = 0; i < n; i++) { // accumulate sum sum += arr[i]; // when subarray starts from index '0' if (sum == k) maxLen = i + 1; // make an entry for 'sum' if it is // not present in 'um' if (um.find(sum) == um.end()) um[sum] = i; // check if 'sum-k' is present in 'um' // or not if (um.find(sum - k) != um.end()) { // update maxLength if (maxLen < (i - um[sum - k])) maxLen = i - um[sum - k]; } } // required maximum length return maxLen; } // function to find the length of the longest // subarray having maximum sum int lenLongSubarrWithMaxSum(int arr[], int n) { int maxSum = maxSubArraySum(arr, n); return lenOfLongSubarrWithGivenSum(arr, n, maxSum); } // Driver program to test above int main() { int arr[] = { 5, -2, -1, 3, -4 }; int n = sizeof(arr) / sizeof(arr[0]); cout << "Length of longest subarray having maximum sum = " << lenLongSubarrWithMaxSum(arr, n); return 0; } |
chevron_right
filter_none
Java
// Java implementation to find // the length of the longest // subarray having maximum sum import java.util.*; class GFG { // function to find the // maximum sum that // exists in a subarray static int maxSubArraySum(int arr[], int size) { int max_so_far = arr[0]; int curr_max = arr[0]; for (int i = 1; i < size; i++) { curr_max = Math.max(arr[i], curr_max + arr[i]); max_so_far = Math.max(max_so_far, curr_max); } return max_so_far; } // function to find the // length of longest // subarray having sum k static int lenOfLongSubarrWithGivenSum(int arr[], int n, int k) { // unordered_map 'um' implemented // as hash table HashMap<Integer, Integer> um = new HashMap<Integer, Integer>(); int sum = 0, maxLen = 0; // traverse the given array for (int i = 0; i < n; i++) { // accumulate sum sum += arr[i]; // when subarray starts // from index '0' if (sum == k) maxLen = i + 1; // make an entry for 'sum' if // it is not present in 'um' if (um.containsKey(sum)) um.put(sum, i); // check if 'sum-k' is present // in 'um' or not if (um.containsKey(sum - k)) { // update maxLength if (maxLen < (i - um.get(sum - k))) maxLen = i - um.get(sum - k); } } // required maximum length return maxLen; } // function to find the length // of the longest subarray // having maximum sum static int lenLongSubarrWithMaxSum(int arr[], int n) { int maxSum = maxSubArraySum(arr, n); return lenOfLongSubarrWithGivenSum(arr, n, maxSum); } // Driver Code public static void main(String args[]) { int arr[] = { 5, -2, -1, 3, -4 }; int n = arr.length; System.out.println("Length of longest subarray " + "having maximum sum = " + lenLongSubarrWithMaxSum(arr, n)); } } // This code is contributed by Arnab Kundu |
chevron_right
filter_none
C#
// C# implementation to find // the length of the longest // subarray having maximum sum using System; using System.Collections.Generic; public class GFG{ // function to find the // maximum sum that // exists in a subarray static int maxSubArraySum(int []arr, int size) { int max_so_far = arr[0]; int curr_max = arr[0]; for (int i = 1; i < size; i++) { curr_max = Math.Max(arr[i], curr_max + arr[i]); max_so_far = Math.Max(max_so_far, curr_max); } return max_so_far; } // function to find the // length of longest // subarray having sum k static int lenOfLongSubarrWithGivenSum(int []arr, int n, int k) { // unordered_map 'um' implemented // as hash table Dictionary<int, int> um = new Dictionary<int, int>(); int sum = 0, maxLen = 0; // traverse the given array for (int i = 0; i < n; i++) { // accumulate sum sum += arr[i]; // when subarray starts // from index '0' if (sum == k) maxLen = i + 1; // make an entry for 'sum' if // it is not present in 'um' if (um.ContainsKey(sum)) um.Add(sum, i); // check if 'sum-k' is present // in 'um' or not if (um.ContainsKey(sum - k)) { // update maxLength if (maxLen < (i - um[sum - k])) maxLen = i - um[sum - k]; } } // required maximum length return maxLen; } // function to find the length // of the longest subarray // having maximum sum static int lenLongSubarrWithMaxSum(int []arr, int n) { int maxSum = maxSubArraySum(arr, n); return lenOfLongSubarrWithGivenSum(arr, n, maxSum); } // Driver Code public static void Main() { int []arr = { 5, -2, -1, 3, -4 }; int n = arr.Length; Console.WriteLine("Length of longest subarray " + "having maximum sum = " + lenLongSubarrWithMaxSum(arr, n)); } } // This code is contributed by Rajput-Ji |
chevron_right
filter_none
Output:
Length of longest subarray having maximum sum = 4
Time Complexity: O(n).
Auxiliary Space: O(n).
Recommended Posts:
- Longest subarray with sum divisible by k
- Longest subarray such that the difference of max and min is at-most one
- Longest subarray with only one value greater than k
- Longest increasing subarray
- Longest Subarray of non-negative Integers
- Longest Subarray having sum of elements atmost 'k'
- Longest subarray in which all elements are greater than K
- Length of the longest Subarray with only Even Elements
- Longest Subarray with Sum greater than Equal to Zero
- Longest subarray not having more than K distinct elements
- Longest Increasing subarray with one change allowed
- Longest subarray with elements having equal modulo K
- Longest Prime Subarray after removing one element
- Longest subarray having difference in the count of 1's and 0's equal to k
- Longest subarray having average greater than or equal to x
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. 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.



