Skip to content

Commit 96d11a7

Browse files
committed
Create Kadane's Algorithm.cpp
1 parent 2c06dd1 commit 96d11a7

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

Kadane's Algorithm.cpp

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//Max sum array problem using Kadane's Algorithm
2+
//Kadane's Algorithm helps us to reduce the for loop
3+
//which are used to find the max sub array sum.
4+
#include<iostream>
5+
using namespace std;
6+
int main()
7+
{
8+
int a[100]={0};
9+
int n;
10+
int cs=0;//cs is the current sum
11+
int ms=0;//ms is the maximum sum
12+
cout<<"Enter the number of elements in the array";
13+
cin>>n;
14+
for(int i=0;i<n;i++)
15+
{
16+
cin>>a[i];
17+
}
18+
for(int i=0;i<n;i++)
19+
{
20+
cs = cs+a[i];
21+
if(cs<0)
22+
{
23+
cs=0;
24+
}
25+
ms=max(cs,ms);
26+
}
27+
cout<<"Maximum Sum"<<ms<<endl;
28+
return 0;
29+
}

0 commit comments

Comments
 (0)