You are on page 1of 5

void MyArray::SizeOfSubarrayWithMaximumSum()

{
//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 }.

cout << SizeOfSubarrayWithMaximumSum(m_arr, 0, m_length - 1);


}

int MyArray::SizeOfSubarrayWithMaximumSum(int* arr, int startIndex, int endIndex)


{ Size of The Subarray With Maximum Sum
int
AnmaxSum
array is =given,
0, maxSumSoFar = 0,
find length of the CounterOfMaxSumSubArray
subarray having maximum sum.= 0, CounterSoFar =0;

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}.
}

if (maxSumSoFar > maxSum)


Recommended:
{ Please try your approach on {IDE} first, before moving on to the solution.
maxSum = maxSumSoFar;
CounterOfMaxSumSubArray = CounterSoFar;
}

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;

int maxSubArraySum(int a[], int size)


{
int max_so_far = INT_MIN, 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);


}

/*Driver program to test maxSubArraySum*/


int main()
{
int a[] = {-2, -3, 4, -1, -2, 1, 5, -3};
int n = sizeof(a)/sizeof(a[0]);
cout << maxSubArraySum(a, n);
return 0;
}
Run on IDE

Java
// Java program to print length of the largest
// contiguous array sum
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);
}

// 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

from sys import maxsize

# Function to find the maximum contiguous subarray


# and print its starting and end index
def maxSubArraySum(a,size):

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_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)

# Driver program to test maxSubArraySum


a = [-2, -3, 4, -1, -2, 1, 5, -3]
print(maxSubArraySum(a,len(a)))
Run on IDE

C#
// C# program to print length of the
// largest contiguous array sum
using System;

class GFG {

// Function to find maximum subarray sum


static int maxSubArraySum(int []a, int size)
{
int max_so_far = int.MinValue,
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);
}
// 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));
}
}

// This code is contributed by parashar...


Run on IDE

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.

Arrays Dynamic Programming Login to Improve this Article

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.

Recommended Posts:

Maximum average sum partition of an array


Find n-variables from n sum equations with one missing
Minimum rooms for m events of n batches with given schedule
Print array elements that are divisible by at-least one other
Minimum operations to make GCD of array a multiple of k
Replace two consecutive equal values with one greater
Count smaller elements on right side using Set in C++ STL
Maximum height of triangular arrangement of array values
Making elements of two arrays same with minimum increment/decrement
Maximum equlibrium sum in an array

(Login to Rate)

Average Difficulty : 3/5.0


3 Based on 2 vote(s)
Add to TODO List
Mark as DONE
Basic Easy Medium Hard Expert

Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.

Load Comments Share this post!

710-B, Advant Navis Business Park,


Sector-142, Noida, Uttar Pradesh - 201305
feedback@geeksforgeeks.org

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

@geeksforgeeks, Some rights reserved

You might also like