Skip to content

Commit c4b43f3

Browse files
committed
max Sum of Sumarray
1 parent a69840b commit c4b43f3

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

MaxSumofSubArray.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* MaxSumofSubArray.cpp
3+
*
4+
* Created on: 23-Jun-2017
5+
* Author: baliyan
6+
*/
7+
#include <iostream>
8+
#include <climits>
9+
#include <stdio.h>
10+
using namespace std;
11+
int maxSumArray(int a[], int size) {
12+
int maxSoFar = INT_MIN;
13+
int maxEndinHere = 0;
14+
for (int i = 0; i < size; i++) {
15+
maxEndinHere = maxEndinHere + a[i];
16+
17+
if (maxSoFar < maxEndinHere) {
18+
maxSoFar = maxEndinHere;
19+
}
20+
if (maxEndinHere < 0)
21+
maxEndinHere = 0;
22+
}
23+
return maxSoFar;
24+
}
25+
26+
int main() {
27+
int a[] = { -2, -3, 4, -1, -2, 1, 5, -3 };
28+
int size = sizeof(a) / sizeof(a[0]);
29+
int maxSum = maxSumArray(a, size);
30+
cout<<"max Sum is ::"<<maxSum<<endl;
31+
}
32+

0 commit comments

Comments
 (0)