Skip to content

Commit

Permalink
Create Kadane_Maximum sum subarray
Browse files Browse the repository at this point in the history
  • Loading branch information
AyushiChakrabarty authored Sep 2, 2020
1 parent 809fc11 commit 63a4afe
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions Kadane_Maximum sum subarray
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//Maximum Sum Subarray_O(n)
#include<cmath>
#include<iostream>
#include<climits>
using namespace std;

int mss(int arr[],int n)
{
int i;
int ans = arr[0],sum = 0;

for(i= 1;i < n; ++i)
ans = max(ans,arr[i]);
if(ans<0)
return ans;
ans = 0;
for(i = 0 ;i < n; ++i)
{
if(sum + arr[i] > 0)
sum+=arr[i];
else
sum = 0;
ans = max(ans,sum);
}
return ans;
}

int main(int argc, char const *argv[])
{
int arr[] = {3,-2,5,-1};
cout<<mss(arr,4)<<"\n";
return 0;
}

0 comments on commit 63a4afe

Please sign in to comment.