-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
17d1f97
commit 5e3d7e3
Showing
1 changed file
with
33 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// kadane's algorithm | ||
|
||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
int max_array_sum(int ar[],int a){ | ||
|
||
int till_cur_sum=0 , total_sum=INT_MIN , max_element=INT_MIN; | ||
for(int j=0;j<a;j++){ | ||
till_cur_sum = till_cur_sum + ar[j]; | ||
total_sum = max(till_cur_sum,total_sum); | ||
// max_element= max(max_element,ar[j]); | ||
if(till_cur_sum<0){ | ||
till_cur_sum=0; | ||
} | ||
} | ||
if(total_sum==0){ | ||
total_sum = max_element; | ||
} | ||
return total_sum; | ||
} | ||
|
||
int main(){ | ||
int n; | ||
cin>>n; | ||
int arr[n]; | ||
|
||
for(int i=0;i<n;i++){ | ||
cin>>arr[i]; | ||
} | ||
cout<<" Maximum subarray sum : "<< max_array_sum(arr,n); | ||
return 0; | ||
} |