Professional Documents
Culture Documents
{
//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
Custom Search
//
// Input : ar[] = { -2, -3, 4, -1, -2, 1, 5, -3 }
Geeks Classes Login
//Output: Length of the subarray is 5
// Explanation : Subarray with
Write an consecutive elements
Article
// and maximum sum will be{ 4, -1, -2, 1, 5 }.
Examples:
for (int i = startIndex; i <= endIndex; ++i)
{
maxSumSoFar
Input += -2,
: a[] = {1, arr[i];
1, 1, -2, 1}
CounterSoFar++;
Output : Length of the subarray is 2
Explanation: Subarray with consecutive elements
and maximum sum will be {1, 1}. So length is 2
if (maxSumSoFar < 0)
Input
{ : ar[] = { -2, -3, 4, -1, -2, 1, 5, -3 }
OutputmaxSumSoFar
: Length of the subarray is 5
= 0;
Explanation: Subarray with consecutive elements
CounterSoFar = 0;
and maximum sum will be {4, -1, -2, 1, 5}.
}
return CounterOfMaxSumSubArray;
}
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.
C++
// C++ program to print length of the largest
// contiguous array sum
#include<iostream>
#include<climits>
using namespace std;
if (max_ending_here < 0)
{
max_ending_here = 0;
s = i + 1;
}
}
Java
// Java program to print length of the largest
// contiguous array sum
class GFG {
if (max_ending_here < 0)
{
max_ending_here = 0;
s = i + 1;
}
}
return (end - start + 1);
}
// Driver code
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));
}
}
Run on IDE
Python3
# Python program to print largest contiguous array sum
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_ending_here < 0:
max_ending_here = 0
s = i+1
C#
// C# program to print length of the
// largest contiguous array sum
using System;
class GFG {
if (max_ending_here < 0)
{
max_ending_here = 0;
s = i + 1;
}
}
return (end - start + 1);
}
// Driver code
public static void Main(String[] args)
{
int []a = {-2, -3, 4, -1, -2, 1, 5, -3};
int n = a.Length;
Console.Write(maxSubArraySum(a, n));
}
}
Output:
bitan2017
Check out this Author's contributed articles.
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 write comments if you find anything incorrect, or you want to share more information about the topic
discussed above.
Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.
Recommended Posts:
(Login to Rate)
Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.
COMPANY LEARN
About Us Algorithms
Careers Data Structures
Privacy Policy Languages
Contact Us CS Subjects
Video Tutorials
PRACTICE CONTRIBUTE
Company-wise Write an Article
Topic-wise Write Interview Experience
Contests Internships
Subjective Questions Videos