-
Notifications
You must be signed in to change notification settings - Fork 164
/
Kadane's algorithm.cpp
69 lines (53 loc) · 957 Bytes
/
Kadane's algorithm.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// Largest sum contiguous sub array using divide and conqure
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
ll max(ll a, ll b, ll c)
{
int res;
if(a>b && a>c)
return a;
else if(b>c && b>a)
return b;
else
return c;
}
ll maxCrossing(int arr[], int n, int l, int h, int m)
{
ll left_sum=INT_MIN, right_sum=INT_MIN, sum=0;
for(int i=m ; i>=l ; i--)
{
sum+=arr[i];
if(sum > left_sum)
left_sum = sum;
}
sum = 0;
for(int i=m+1 ; i<=h ; i++)
{
sum+=arr[i];
if(sum > right_sum)
right_sum = sum;
}
return max(right_sum, left_sum, left_sum+right_sum);
}
ll maxSum(int arr[], int n, int l, int h)
{
if(l == h)
return arr[l];
int m = (l+h)/2;
return max(maxSum(arr, n, l, m), maxSum(arr, n, m+1, h), maxCrossing(arr, n, l, h, m));
}
void solve()
{
int n;
cin>>n;
int arr[n];
for(int i=0 ; i<n ; i++)
cin>>arr[i];
cout<<maxSum(arr, n, 0, n-1)<<endl;
}
int main()
{
solve();
return 0;
}