Java Program for Size of The Subarray With Maximum Sum
An array is given, find the 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 the Largest Sum Contiguous Subarray Problem.
The idea is to update starting index whenever the sum ending here becomes less than 0.
Java
class GFG {
static int maxSubArraySum( int a[], int size)
{
int max_so_far = Integer.MIN_VALUE,
max_ending_here = 0 ,start = 0 ,
end = 0 , s = 0 ;
for ( int i = 0 ; i < size; i++)
{
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 );
}
public static void main(String[] args)
{
int a[] = { - 2 , - 3 , 4 , - 1 , - 2 , 1 , 5 , - 3 };
int n = a.length;
System.out.println(maxSubArraySum(a, n));
}
}
|
Time Complexity: O(N) where N is size of the input array. This is because a for loop is executed from 1 to 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:
Java
import java.util.*;
public class GFG {
static int maxSubarraySum(List<Integer> a)
{
int n = a.size();
int max_sum = a.get( 0 );
int current_sum = a.get( 0 );
int start = 0 ;
int end = 0 ;
int max_start = 0 ;
int max_end = 0 ;
for ( int i = 1 ; i < n; i++) {
if (a.get(i) > current_sum + a.get(i)) {
start = i;
}
current_sum = Math.max(a.get(i),
current_sum + a.get(i));
if (current_sum > max_sum) {
max_sum = current_sum;
end = i;
max_start = start;
max_end = end;
}
}
return max_end - max_start + 1 ;
}
public static void main(String[] args)
{
List<Integer> a
= Arrays.asList(- 2 , - 3 , 4 , - 1 , - 2 , 1 , 5 , - 3 );
System.out.println(maxSubarraySum(a));
}
}
|
Time Complexity: O(n), where n is the 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!
Last Updated :
10 May, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...