Skip to content

Commit 528fa42

Browse files
authored
Merge pull request #16 from ritmojs/noob
maximum-subarray Problem
2 parents ec96a20 + 969d597 commit 528fa42

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

Arrays-algos/maximum-subarray-sum.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// C++ program to print largest contiguous array sum
2+
#include<iostream>
3+
#include<climits>
4+
using namespace std;
5+
6+
int maxSubArraySum(int a[], int size)
7+
{
8+
int max_so_far = INT_MIN, max_ending_here = 0;
9+
10+
for (int i = 0; i < size; i++)
11+
{
12+
max_ending_here = max_ending_here + a[i];
13+
if (max_so_far < max_ending_here)
14+
max_so_far = max_ending_here;
15+
16+
if (max_ending_here < 0)
17+
max_ending_here = 0;
18+
}
19+
return max_so_far;
20+
}
21+
22+
/*Driver program to test maxSubArraySum*/
23+
int main()
24+
{
25+
int a[] = {-2, -3, 4, -1, -2, 1, 5, -3};
26+
int n = sizeof(a)/sizeof(a[0]);
27+
int max_sum = maxSubArraySum(a, n);
28+
cout << "Maximum contiguous sum is " << max_sum;
29+
return 0;
30+
}

0 commit comments

Comments
 (0)