From 74a8bb348c314df779f7ccd86e680027261945da Mon Sep 17 00:00:00 2001 From: Ayushi C <67193440+AyushiChakrabarty@users.noreply.github.com> Date: Thu, 17 Sep 2020 23:19:50 +0530 Subject: [PATCH] Create Divide and Conquer_Maximum Sum Subarray --- Divide and Conquer_Maximum Sum Subarray | 37 +++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 Divide and Conquer_Maximum Sum Subarray diff --git a/Divide and Conquer_Maximum Sum Subarray b/Divide and Conquer_Maximum Sum Subarray new file mode 100644 index 0000000..dd3ff71 --- /dev/null +++ b/Divide and Conquer_Maximum Sum Subarray @@ -0,0 +1,37 @@ +#include +#include +#include +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<