Loading...
Compute the maximum contiguous subarray sum using the divide & conquer strategy. Illustrates combining local segment solutions via a crossing structure (suffix + prefix) and contrasts with linear DP (Kadane). Useful for mastering recurrence derivation and boundary reasoning.
T(n)=2T(n/2)+O(n) → O(n log n)
1function maxSubarray(arr):2 if length(arr) == 1: return arr[0]3 mid = length(arr)//24 leftBest = maxSubarray(arr[0:mid])5 rightBest = maxSubarray(arr[mid:])6 crossBest = bestCrossing(arr, mid)7 return max(leftBest, rightBest, crossBest)