Short answer: Kadane’s algorithm finds the contiguous subarray of maximum sum in O(n) time and O(1) extra memory. It walks the array once, keeping the best sum that ends at the current index and the best sum seen anywhere. The sum that ends here is the current element plus the previous ending sum, unless that previous sum is negative, in which case the current element starts a new subarray. An array of all negative numbers needs a stated rule: the largest element, if an empty subarray is forbidden.
What the problem actually asks
A brute force over all subarrays is Θ(n²). The optimal subarray that ends at index i either extends the optimal subarray that ends at i−1 or starts fresh at i. It extends only when the sum through i−1 is positive. A negative ending sum cannot help any later element, so it is discarded. That case split is the whole algorithm.
The global answer is the maximum of those ending sums. Forgetting the global maximum and returning only the last ending sum returns a suffix, not the best subarray.
What the algorithm guarantees
With the empty subarray forbidden, one pass returns the maximum subarray sum and, with two index variables, the bounds of one such subarray. Time is Θ(n) and extra memory is a handful of scalars. The algorithm is correct for any integers or reals. It is not correct for a maximum product, because a negative factor can become useful later and the “reset when negative” rule throws that factor away. Maximum subarray product keeps a running maximum and a running minimum.
If every element is negative and the empty subarray is allowed, the answer is 0. If it is forbidden, the answer is the largest element, which the reset rule already returns provided the running best is initialized to negative infinity rather than to zero. Initializing the best to zero silently allows the empty subarray.
The working steps
- Set best to negative infinity and the sum ending here to 0, if an empty subarray is forbidden and the array is nonempty. If the empty subarray is allowed, initialize best to 0.
- For each element x, set the ending sum to max(x, ending sum + x). Then set best to max(best, ending sum).
- The ending-sum update is the reset: a negative previous sum loses to x alone.
- To recover indices, remember the start of the current ending subarray and snapshot the start and end whenever best improves.
- Do not use this reset for a product. Track both a max and a min ending product, and swap roles when a negative element arrives.
The linear scan is for a contiguous subarray of a one-dimensional array. A subsequence that may skip elements has a different optimum, often the sum of all positive elements. A two-dimensional maximum subarray is a different, heavier DP. Maximum product is the paired running min and max.
Approaches that look simpler and fail
| Approach | Why it fails when the best sum is initialized to zero on a negative array |
|---|---|
| Initialize best to 0 and forbid the empty subarray | An all-negative array then returns 0, which is the empty sum. The forbidden-empty rule needs a best initialized below every element, and the answer is the largest element. |
| Reset whenever the ending sum is positive | The reset condition is the opposite. A positive ending sum is exactly what should be extended. |
| Return the last ending sum | The best subarray may have ended earlier. The algorithm tracks a separate global maximum. |
| Apply the same reset to a maximum product | A negative running product can become the maximum after one more negative factor. Discarding it loses the optimum. |
A longest increasing subsequence is patience sorting, not a sum. A maximum subarray product keeps two running values. Kadane is the contiguous maximum sum.
A working example
The Bash script and the Python script run Kadane on a nonempty array. The best ending here is max(x, ending+x). Both print 6.
a=(-2 1 -3 4 -1 2 1 -5 4)
best=${a[0]}
ending=${a[0]}
for ((i=1; i<${#a[@]}; i++)); do
x=${a[i]}
if (( x > ending + x )); then ending=$x; else ending=$((ending+x)); fi
if (( ending > best )); then best=$ending; fi
done
echo "$best"
a = [-2, 1, -3, 4, -1, 2, 1, -5, 4]
best = ending = a[0]
for x in a[1:]:
ending = max(x, ending + x)
best = max(best, ending)
assert best == 6
print(best)
Where it is used
Kadane’s algorithm finds the contiguous segment of a time series with the largest sum: the best run of returns, the brightest strip of a signal. One buy and one sell on a price series is a close relative. A segment that may be empty is a different boundary condition.
When you can drop this algorithm
Drop Kadane when the subarray may be empty and the array is otherwise unconstrained. The answer is the maximum of 0 and the Kadane value, which is the zero initialization. Keep the negative-infinity initialization when a nonempty subarray is required.
Why this stays published
We publish this so a search for Kadane’s algorithm returns the reset on a negative ending sum and the initialization that decides whether the empty subarray is allowed. Kadane’s algorithm finds a maximum-sum contiguous subarray in one pass by extending a positive ending sum and starting over after a negative one.
本文由 HoHo 與 AI 協作整理,最後更新於 2026 年 11 月 13 日。
