O(logn) solution using divide and conquer algorithm


#1

Comment body goes here.int ma(vector&a,int s,int e)
{
if(s==e)
return a[s];
if(s+1==e)
return max(a[s],a[e]);
int mid=(s+e)/2;
return max(ma(a,s,mid),ma(a,mid+1,e));
}
int mi(vector&a,int s,int e)
{
if(s==e)
return a[s];
if(s+1==e)
return min(a[s],a[e]);
int mid=(s+e)/2;
return min(mi(a,s,mid),mi(a,mid+1,e));
}
int Solution::solve(vector &A) {
int n=A.size();
return ma(A,0,n-1)+mi(A,0,n-1);
}