how to find the suarray with maximum sum using divide and conquer

Solutions on MaxInterview for how to find the suarray with maximum sum using divide and conquer by the best coders in the world

showing results for - "how to find the suarray with maximum sum using divide and conquer"
Elena
04 May 2018
1#include <stdio.h>
2#include <limits.h>
3 
4// Utility function to find maximum of two numbers
5int max(int x, int y) {
6    return (x > y) ? x : y;
7}
8 
9// Function to find maximum subarray sum using divide and conquer
10int maximum_sum(int A[], int low, int high)
11{
12    // If array contains only one element
13    if (high == low)
14        return A[low];
15 
16    // Find middle element of the array
17    int mid = (low + high) / 2;
18 
19    // Find maximum subarray sum for the left subarray
20    // including the middle element
21    int left_max = INT_MIN;
22    int sum = 0;
23    for (int i = mid; i >= low; i--)
24    {
25        sum += A[i];
26        if (sum > left_max)
27            left_max = sum;
28    }
29 
30    // Find maximum subarray sum for the right subarray
31    // excluding the middle element
32    int right_max = INT_MIN;
33    sum = 0;    // reset sum to 0
34    for (int i = mid + 1; i <= high; i++)
35    {
36        sum += A[i];
37        if (sum > right_max)
38            right_max = sum;
39    }
40 
41    // Recursively find the maximum subarray sum for left subarray
42    // and right subarray and take maximum
43    int max_left_right = max(maximum_sum(A, low, mid),
44                            maximum_sum(A, mid + 1, high));
45 
46    // return maximum of the three
47    return max(max_left_right, left_max + right_max);
48}
49 
50// Maximum Sum Subarray using Divide & Conquer
51int main(void)
52{
53    int arr[] = { 2, -4, 1, 9, -6, 7, -3 };
54    int n = sizeof(arr) / sizeof(arr[0]);
55 
56    printf("The maximum sum of the subarray is %d", 
57            maximum_sum(arr, 0, n - 1));
58 
59    return 0;
60}
61
queries leading to this page
maximum sum subarray c code divide n conquerlargest sum contiguous subarray using divide and conquermaximum subarray sum divide and conquermaximum subarray sum 28divide and conquer 29maximum subarray sum divide and conquer time complexityfind maximum subarray divide and conquerkadane algorithm using divide and conquermaximum subarray sum with multiplication pythonmax sub sum array divide and conquerkadane 27s algorithm divide and conquerlargest sub array sum recursionlargest subarray with zero summax subarray sum divide and conquerdivide and conquer max sub arrayabout maximum subarray sum using divide and conquerdivide array into two parts such that difference of sum is minimummaximum sum contiguous subarray using divide and conquermaximum subarray sum using devide and conquerdivide and conquer maximum subarray summaximum sum subarray using divide and conquerlargest sum contiguous subarray divide and conquermaximum subarray divide and conquersum list using divide and conquer algorithm in pythonmaximum subarray problem divide and conquermaximum subarray sum using divide and conquer algorithmmaximum subarray problem using divide and conquerdetermine the maximum subarray sum using the divide and conquer approach for 6 elements arraymaximum subarray sum by divide and conquerdivide and conquer algorithm to find maximum sum subarraycalculation sum using divide and conquer algorithmmax subarray sum divide and conquer algorithmmaximum subarray divide and conquer c 2b 2bsubarray max in nlognmax subarray c 23 divide and conquerdivide and conquer approach to finding a maximal sum subvector in an arraysubarray max element in nlogndivide and conquer max subarray linear timedivide and conquer sum of arraymax sum subarray divide and conquermaximum subarray sum using divide and conquerdetermine the maximum subarray sum using the divide and conquer approach for 6 elements array simulationmaximum subarray sum using divide and conquer algorithm timecomplexitymax sum subarray nlognmaximum subarray using divide and conquermaximum sum subarray divide and conquerfind max sum in array divide by 3maximum subarray sum in o 28nlogn 29max subarray divide and conquerdetermine the maximum subarray sum using the divide and conquer approach formaximum sum subarray divide and conquer solutionsum of array divide and conquermaximum sum subarray divide and conquer in pythonmaximum sum subarray divide and conquer real life problemsdivide and conquer maximum subarraymaximum subarray sum using the divide and conquer approach for 6 elements array simulationmaximum sum subarray divide and conquer pythonhow to find the suarray with maximum sum using divide and conquer