AlgoScope

Maximum Subarray

AlgorithmintermediateArray Techniques

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
active 1-1-2011-3243-142516-5748i

The run so far totals -2, and dragging that along would only hurt. Start over at 1. That is a new best.

Open in the player →or start at step 2

What you will see

Compare brute force, divide and conquer and Kadane on the same array.

How prefix sum and kadane works →

Cost

BestO(n)
AverageO(n)
WorstO(n)
SpaceO(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.

Taught by the same lesson

Prefix Sum and Kadane covers these too, in the same run.