Skip to content

Commit 967d6d2

Browse files
authored
maxSumaraayy
1 parent c95666d commit 967d6d2

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

maxSumArray

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 <bits/stdc++.h>
3+
using namespace std;
4+
5+
int maxSubArraySum(int a[], int size)
6+
{
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+
// Driver Code
21+
int main()
22+
{
23+
int a[] = { -2, -3, 4, -1, -2, 1, 5, -3 };
24+
int n = sizeof(a) / sizeof(a[0]);
25+
26+
// Function Call
27+
int max_sum = maxSubArraySum(a, n);
28+
cout << "Maximum contiguous sum is " << max_sum;
29+
return 0;
30+
}

0 commit comments

Comments
 (0)