Skip to content

Commit

Permalink
added a cpp file for kadane algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
Neeltyper001 committed Oct 23, 2022
1 parent e29f432 commit fb50192
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions data_structures/Arrays/CPP/kadaneAlgo.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// kadane's algo

#include<iostream>

using namespace std;

int maximum_subarray_sum(int arr[] , int n){

int cs = 0;
int largest =0;

for(int i=0; i<n ; i++){
cs = cs + arr[i];

if(cs < 0){
cs = 0;
}

largest = max(largest , cs);
}

return largest;
}

int main()
{
int arr[] = { -2,3 ,4 -1 , 5, -12 , 6,1,3};
int n = sizeof(arr)/sizeof(int);
cout<< "The maximum sum is "<<maximum_subarray_sum(arr , n) <<endl;

}

0 comments on commit fb50192

Please sign in to comment.