-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathKadane-algo.cpp
40 lines (32 loc) · 852 Bytes
/
Kadane-algo.cpp
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
#include<bits/stdc++.h>
using namespace std;
class Solution{
public:
// arr: input array
// n: size of array
//Function to find the sum of contiguous subarray with maximum sum.
long long maxSubarraySum(int arr[], int n){
// Your code here
long long int cSum=0, maxSum=arr[0];
for(long long int i=0; i<n; i++)
{
cSum+=arr[i];
maxSum=(cSum>maxSum ? cSum : maxSum);
if(cSum<0) cSum=0;
}
return maxSum;
}
};
int main()
{
int t,n;
cin>>t; //input testcases
while(t--) //while testcases exist
{
cin>>n;
int a[n];
for(int i=0;i<n;i++) cin>>a[i];
Solution ob;
cout << ob.maxSubarraySum(a, n) << endl;
}
}