Skip to content

Commit

Permalink
Merge pull request #3432 from Neeltyper001/master
Browse files Browse the repository at this point in the history
added a cpp file for kadane algorithm
  • Loading branch information
ZoranPandovski committed Oct 25, 2022
2 parents faae290 + fb50192 commit 3c900af
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 3c900af

Please sign in to comment.