-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathkadanes-algorithm.cpp
More file actions
executable file
·40 lines (40 loc) · 902 Bytes
/
kadanes-algorithm.cpp
File metadata and controls
executable file
·40 lines (40 loc) · 902 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
//http://practice.geeksforgeeks.org/problems/kadanes-algorithm/0
//
#include<bits/stdc++.h>
using namespace std;
int kadane(int arr[],int n){
int max_ending_here = 0 , max_so_far = -INT_MAX;
for(int i=0;i<n;i++){
max_ending_here += arr[i];
if(max_ending_here < 0){
max_ending_here = 0;
}
if(max_so_far < max_ending_here){
max_so_far = max_ending_here;
}
}
return max_so_far;
}
int main(){
int t,n;
int arr[10005];
cin>>t;
while(t--){
cin>>n;
int ncount = 0;
int mx = -INT_MAX;
for(int i=0;i<n;i++){
cin>>arr[i];
if(arr[i]<0) ncount++;
if(arr[i]>mx)mx = arr[i];
}
if(ncount == n){
cout<<mx<<endl;
}
else{
int res = kadane(arr,n);
cout<<res<<endl;
}
}
return 0;
}