Skip to content

Commit dc20efb

Browse files
authored
Merge pull request ashutosh97#91 from arc9693/kandaneImplementationinC++
Added c++ solution of kadane's algo
2 parents 681bdf9 + c19e009 commit dc20efb

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

Kadane's Algorithm/kadane.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#include<iostream>
2+
#include <bits/stdc++.h>
3+
using namespace std;
4+
5+
int maxSubarraySum(vector<int> a, int l) {
6+
int max = INT_MIN, max_here = 0;
7+
8+
for (int i = 0; i < l; i++) {
9+
max_here = max_here + a[i];
10+
if (max < max_here) max = max_here;
11+
if (max_here < 0) max_here = 0;
12+
}
13+
14+
return max;
15+
}
16+
17+
18+
int main() {
19+
vector<int> arr({-2, -3, 4, -1, -2, 1, 5, -3});
20+
int l = arr.size();
21+
22+
int m = maxSubarraySum(arr, l);
23+
24+
cout<<m<<" is the max subarray sum\n";
25+
26+
return 0;
27+
}

0 commit comments

Comments
 (0)