Skip to content

Commit

Permalink
Create Divide and Conquer_Maximum Sum Subarray
Browse files Browse the repository at this point in the history
  • Loading branch information
AyushiChakrabarty authored Sep 17, 2020
1 parent 7bd0d51 commit 74a8bb3
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions Divide and Conquer_Maximum Sum Subarray
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include<cmath>
#include<iostream>
#include<climits>
using namespace std;

int Max_Subarray_Sum(int arr[],int n)
{
if(n==1)
{
return arr[0];
}
int m = n/2;
int left_MSS = Max_Subarray_Sum(arr,m);
int right_MSS = Max_Subarray_Sum(arr+m,n-m);
int leftsum = INT_MIN, rightsum = INT_MIN, sum=0;
for(int i=m;i < n; i++)
{
sum += arr[i];
rightsum = max(rightsum,sum);
}
sum = 0;
for(int i= (m-1);i >= 0; i--)
{
sum += arr[i];
leftsum = max(leftsum,sum);
}
int ans = max(left_MSS,right_MSS);
return max(ans,leftsum+rightsum);
}


int main(int argc, char const *argv[])
{
int arr[] = {3,-2,5,-1};
cout<<Max_Subarray_Sum(arr,4)<<"\n";
return 0;
}

0 comments on commit 74a8bb3

Please sign in to comment.