0% found this document useful (0 votes)
19 views

Maximum Subarray Sum

The document discusses using a divide and conquer approach to find the maximum sum of a subarray in an integer array. It divides the array into halves, finds the maximum subarray sum for each half recursively, and also checks sums that span the middle element, returning the largest. The time complexity is improved to O(n log n) compared to a naive O(n^2) solution.

Uploaded by

Kritika Singh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Maximum Subarray Sum

The document discusses using a divide and conquer approach to find the maximum sum of a subarray in an integer array. It divides the array into halves, finds the maximum subarray sum for each half recursively, and also checks sums that span the middle element, returning the largest. The time complexity is improved to O(n log n) compared to a naive O(n^2) solution.

Uploaded by

Kritika Singh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Maximum Subarray Sum (Divide and Conquer

Given an integer array, find the maximum sum among all subarrays possible.

The problem differs from the problem of finding the maximum subsequence sum. Unlike
subsequences, subarrays are required to occupy consecutive positions within the original
array.

For example,

Input: nums[] = [2, -4, 1, 9, -6, 7, -3]

Output: The maximum sum of the subarray is 11 (bold)


A naive solution is to consider every possible subarray, find the sum, and take the maximum.

The problem with this approach is that its worst-case time complexity is O(n2), where n is the
size of the input.

Divide and Conquer Approach

Divide and Conquer technique to find the maximum subarray sum. The algorithm works as
follows:
 Divide the array into two equal subarrays.
 Recursively calculate the maximum subarray sum for the left subarray,
 Recursively calculate the maximum subarray sum for the right subarray,
 Find the maximum subarray sum that crosses the middle element.
 Return the maximum of the above three sums.

The time complexity of the above divide-and-conquer solution is O(n.log(n)) as for the given
array of size n, we make two recursive calls on input size n/2 and finding the maximum
subarray crosses midpoint takes O(n) time in the worst case.

Therefore,

T(n) = 2T(n/2) + O(n) = O(n.log(n))


Divide and Conquer Code Snippet:

int maximum_sum(int nums[], int low, int high)


{
if (high <= low) {
return nums[low];
}

int mid = (low + high) / 2;

int left_max = INT_MIN;


int sum = 0;
for (int i = mid; i >= low; i--)
{
sum += nums[i];
if (sum > left_max) {
left_max = sum;
}
}
int right_max = INT_MIN;
sum = 0;
for (int i = mid + 1; i <= high; i++)
{
sum += nums[i];
if (sum > right_max) {
right_max = sum;
}
}

int max_left_right = max(maximum_sum(nums, low, mid),


maximum_sum(nums, mid + 1, high));

return max(max_left_right, left_max + right_max);


}

You might also like