Skip to content

Commit 147bcfa

Browse files
authored
Create kadanealgorithm.cpp
1 parent eebf8c6 commit 147bcfa

1 file changed

Lines changed: 27 additions & 0 deletions

File tree

kadanealgorithm.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*Implementing Kadane's algorithm -finding the largest sum of a subarray in an array*/
2+
3+
#include <iostream>
4+
using namespace std;
5+
6+
int main() {
7+
int n;//to store array size
8+
int s=0;//storing sum of a subarray
9+
int max=-2147483648;//storing maximum sum of a subarray
10+
cin>>n; //Entering array size
11+
int a[n+1];
12+
for(int i=0;i<n;i++)//Enterinh elements of array
13+
{cin>>a[i];}
14+
15+
for(int i=0;i<n;i++)
16+
{s=s+a[i];
17+
if(s<a[i])
18+
{s=a[i];}
19+
if(s>max)
20+
max=s;
21+
}
22+
cout<<max<<endl;
23+
24+
25+
26+
return 0;
27+
}

0 commit comments

Comments
 (0)