Skip to content

Commit 200e536

Browse files
authored
Kadane's Algorithm added
1 parent 4bc46a7 commit 200e536

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//Given an array of intergers. Find the contiguous sub-array with maximum sum.
2+
3+
#include <bits/stdc++.h>
4+
using namespace std;
5+
6+
int main() {
7+
cout<<"Enter the number of elements for array:\n";
8+
int n;
9+
cin>>n;
10+
11+
vector<int>v(n);
12+
cout<<"Enter the values of the array:\n";
13+
for(int i=0;i<n;i++){
14+
cin>>v[i];
15+
}
16+
int ans = INT_MIN;
17+
int sum = 0;
18+
for(int i=0;i<n;i++){
19+
sum = max(v[i], sum + v[i]);
20+
ans = max(ans, sum);
21+
}
22+
23+
cout<<"The maximum sum of contigous array is: ";
24+
cout<<ans<<"\n";
25+
return 0;
26+
}

0 commit comments

Comments
 (0)