Maximum Subarray
The largest total of any run of neighbours. Kadane solves it in one pass.
Decision · step 2 of 10Prefix Sum and Kadane: Kadane on a mixed array
The run so far totals -2, and dragging that along would only hurt. Start over at 1. That is a new best.
What you will see
Compare brute force, divide and conquer and Kadane on the same array.
Cost
| Best | O(n) |
|---|---|
| Average | O(n) |
| Worst | O(n) |
| Space | O(1) |
Divide and conquer variant is O(n log n).
How you work with it here
play it through, step one change at a time, scrub to any step, run it on your own input, predict what happens next.
Screen readers: Each cell is announced as index, value and state; each step's accessibility text states the decision and its effect.
Reduced motion: Swaps and shifts become value crossfades with an outline flash; pointers appear at their destination.
Before this
Taught by the same lesson
Prefix Sum and Kadane covers these too, in the same run.