Purpose: Find the maximum sum of a contiguous subarray in an array, in O(n) time.
Algorithm
- Keep a running sum,
curSum, starting at 0. KeepmaxSumto track the best answer, starting at the smallest possible value (orarr[0]). - Traverse the array left to right.
- Add the current element to
curSum. - Update
maxSumifcurSumis bigger. - If
curSumbecomes negative, reset it to 0 (a negative running sum only drags down future sums, so drop it and start fresh). - After traversal,
maxSumis the answer.
Code
int kadane(vector<int>& arr) {
int curSum = 0;
int maxSum = arr[0];
for (int i = 0; i < arr.size(); i++) {
curSum += arr[i];
maxSum = max(maxSum, curSum);
if (curSum < 0) curSum = 0;
}
return maxSum;
}Paradigm
Dynamic Programming. curSum at index i represents the optimal solution to the subproblem βbest subarray sum ending at i,β built from the optimal solution at i-1 via a simple recurrence β a classic 1D DP, space-optimized from O(n) to O(1).
Complexity
- Time: O(n)
- Space: O(1)
Proof of Correctness
Claim: Resetting curSum to 0 whenever it goes negative never loses the optimal answer.
Reasoning: If curSum becomes negative after including some prefix, that prefix is actively hurting any subarray sum itβs part of. Any future subarray that starts after this point and includes this negative curSum would do strictly better by excluding it and starting fresh. So dropping it (curSum = 0) never removes the true maximum β it only removes sums that could never be optimal.
Since maxSum is updated at every step with the best curSum seen so far, and curSum always represents the best sum of a subarray ending at the current index (given the reset rule), maxSum correctly converges to the maximum subarray sum by the end of traversal. β
Variants / Use Cases
- Max sum circular subarray β
max(Kadane(arr), total - minSubarraySum) - Max product subarray β track both max and min ending here (negatives flip sign)
- Max sum rectangle in 2D matrix β fix row range, collapse to 1D column sums, apply Kadane
- Max subarray with at most/exactly K elements β sliding window + Kadane-like update
- Stock buy-sell (max profit, one transaction) β Kadane on price differences
- Print the actual subarray β track start/end indices during the scan