Loading…
Loading…
Charting your lesson…
Opening your lesson…
Kadane's Algorithm and Maximum Subarray
Lesson 5 of 8
6 steps•about 11 min•11 flashcards
Step 1/ 61 min
"Find the contiguous subarray with the largest sum" is a rite-of-passage interview question, and Kadane's algorithm answers it in a single O(n) pass with O(1) memory — a beautiful example of dynamic programming compressed to two variables.
Core idea: as you scan left to right, ask at each element one local question — "is it better to extend the best subarray ending here, or start fresh at this element?" The global answer is the best of all these local bests.
Step 2/ 61 min
Track cur = the largest sum of a subarray ending at the current index, and best = the largest seen anywhere.
cur = best = a[0]
for i in 1..n-1:
cur = max(a[i], cur + a[i]) # start fresh vs extend
best = max(best, cur)
return best
One pass, constant memory → O(n) time, O(1) space.
cur + a[i] extends the previous best-ending-here; a[i] alone abandons it. If cur is negative, dragging it forward only hurts, so we drop it — that is the DP recurrence dp[i] = max(a[i], dp[i-1] + a[i]). Kadane just stores dp[i-1] in one variable. The answer is max(dp[i]), not dp[n-1], so we track best separately.
best = a[0] (not 0), or you will wrongly return 0 for an empty subarray. This is the most-tested edge case.cur reset (start) and where best improved (bestStart..i).max(normalKadane, totalSum − minSubarray), handling the wrap-around.Step 3/ 61 min
[-2, 1, -3, 4, -1, 2, 1, -5, 4]. Trace cur: -2, 1, -2, 4, 3, 5, 6, 1, 5. The peak cur is 6, from the subarray [4, -1, 2, 1]. best = 6. Notice cur reset to 1 at index 1 (dropping -2) and to 4 at index 3 (dropping the negative run) — those resets are the algorithm's whole intelligence.
Step 4/ 61 min
Any "maximum/minimum sum (or product) of a contiguous subarray" prompt. Recognise it when a problem reduces to "best running total with the option to restart". If the subarray must be non-contiguous, it is a different (usually simpler) problem — just sum the positives.
Step 5/ 61 min
a[0], never 0, unless empty subarrays are explicitly allowed.cur goes negative, the next element will restart it — that is expected.Initialising best = 0. On an all-negative array like [-3, -1, -2] the true answer is -1 (the least-bad single element), but starting from 0 returns 0, which corresponds to an empty subarray the problem usually disallows.
Step 6/ 6· Worked examples1 min
Problem
Find the maximum contiguous subarray sum of [-2, 1, -3, 4, -1, 2, 1, -5, 4] using Kadane's algorithm.
Try it on paper first.
Recall
Keep this
Usage: track the running best-ending-here sum and reset when it turns negative.
Maximum Subarray Sum is one of the most-asked interview questions, and Kadane's algorithm is its elegant O(n) answer. It is the canonical example of "local optimal choice → global optimum" and a gentle on-ramp to dynamic programming.
Walk left to right keeping curMax = the best subarray sum ending at the current index. At each element choose to extend the previous subarray or start fresh:
curMax = max(arr[i], curMax + arr[i]); then globalMax = max(globalMax, curMax).
The insight: if the running sum ever goes negative, it can only hurt what comes next, so drop it and restart. One pass, O(1) memory.
| Variant | Time | Space | Note |
|---|---|---|---|
| Kadane (max sum) | O(n) | O(1) | classic |
| Track indices | O(n) | O(1) | remember start/end |
| Max product subarray | O(n) | O(1) | track min & max |
| All-negative array | O(n) | O(1) | answer = max element |
| Circular max subarray | O(n) | O(1) | max(kadane, total − minKadane) |
"Maximum subarray sum", "max product subarray", or a circular twist. Interviewers expect the O(n) one-pass solution and correct handling of the all-negative edge case.
Kadane: curMax = max(arr[i], curMax+arr[i]), globalMax tracks the best — O(n)/O(1). Init from arr[0], not 0. Products need min & max; circular arrays subtract the minimum subarray from the total.
You've read 0 of 6 steps
Now lock it in — questions straight after reading are what make it stick.