Skip to content

Commit 553b8b5

Browse files
committed
largest contiguous array sum solution added in dp folder.
1 parent 7a12921 commit 553b8b5

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// largest contiguous array sum
2+
#include <bits/stdc++.h>
3+
4+
using namespace std;
5+
6+
int maxSubArraySum(int a[], int size) {
7+
int max_so_far = INT_MIN, max_ending_here = 0;
8+
9+
for (int i = 0; i < size; i++) {
10+
max_ending_here = max_ending_here + a[i];
11+
if (max_so_far < max_ending_here)
12+
max_so_far = max_ending_here;
13+
14+
if (max_ending_here < 0)
15+
max_ending_here = 0;
16+
}
17+
return max_so_far;
18+
}
19+
20+
int main() {
21+
int a[] = {-2, -3, 4, -1, -2, 1, 5, -3};
22+
int n = sizeof(a) / sizeof(a[0]);
23+
24+
int max_sum = maxSubArraySum(a, n);
25+
cout << "Maximum contiguous sum is " << max_sum;
26+
return 0;
27+
}

0 commit comments

Comments
 (0)