-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMaxsumsubarrayd&c.cpp
54 lines (50 loc) · 1012 Bytes
/
Maxsumsubarrayd&c.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
#include <iostream>
using namespace std;
int maxleftsum(int a[],int left,int mid)
{
int sum=0,maxsum=a[mid];
for(int l=mid;l>=left;l--)
{
sum+=a[l];
if(maxsum<sum)
maxsum=sum;
}
return maxsum;
}
int maxrightsum(int a[],int mid,int right)
{
int sum=0,maxsum=a[mid+1];
for(int r=mid+1;r<=right;r++)
{
sum+=a[r];
if(maxsum<sum)
maxsum=sum;
}
return maxsum;
}
int maxsumsubarray(int a[],int left,int right)
{
if(left==right)
return a[left];
int mid=(left+right)/2;
int p,q,ans,r;
p=maxsumsubarray(a,left,mid);
q=maxsumsubarray(a,mid+1,right);
if(p>q)
ans=p;
else
ans=q;
r=maxleftsum(a,left,mid)+maxrightsum(a,mid,right);
if(ans<r)
ans=r;
return ans;
}
int main()
{
int n,a[100];
cin>>n;
for(int i=0;i<n;i++)
cin>>a[i];
cout<<maxsumsubarray(a,0,n-1);
return 0;
}