We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent eebf8c6 commit 147bcfaCopy full SHA for 147bcfa
1 file changed
kadanealgorithm.cpp
@@ -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