-2
0
1
1
-3
2
4
3
-1
4
2
5
1
6
-5
7
4
8
AlgoPlus//structures / kadane
Read the theory

Kadane's Algorithm · Max Subarray

One pass, dropping the running sum whenever it turns negative.

Stability
In-Place
Space Complexity
Avg Time
Legend
Current run
Best subarray
AI Tutor Workspace
In a nutshell
Kadane's algorithm finds the highest-summing stretch of neighbouring numbers in one left-to-right pass. It keeps a running total of the current stretch; the moment that total turns negative it would only drag the next numbers down, so it throws it away and starts a new stretch. The biggest running total ever seen is the answer.
Ready
Press play to begin the cinematic walkthrough.
Walk the array keeping a running sum; whenever it drops below zero, start fresh. The best running sum you ever see is the maximum subarray.
Key terms
Go deeper in the lesson
Read the full theory, intuition & complexity for Kadane's Algorithm · Max Subarray.