Maximum Subarray Sum
Maximum Subarray Sum
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,
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 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,