Kadane's Algorithm to Maximum Sum Subarray Problem
Channel: CS Dojo
Watch on YouTube →Algorithm • medium
Kadane's algorithm finds the maximum-sum contiguous subarray in **O(n) time and O(1) space** replacing the obvious O(n²) brute force of trying every subarray.
The key insight: at each index, the best subarray ending here is either **just this element** or **the best subarray ending at i−1 plus this element**. Whichever is larger.
It's the textbook introductory example of dynamic programming on arrays every DP class teaches it, and it shows up in surprising disguises in real problems.
Typical Complexity Baseline
| Metric | Value |
|---|---|
| Time | O(n) |
| Space | O(1) |
See It
A small animated SVG that illustrates the core idea the moving parts you need to keep in your head when you read the rest of the guide.
If the running sum drops below the current value, restart the window from here.
Watch First
A quick visual walkthrough before the detailed sections pick the format that helps you learn fastest.
Channel: CS Dojo
Watch on YouTube →Mechanics
The building blocks and terminology you need before everything else clicks. Skim or deep-read.
What it is Best subarray sum ending at the current index. The DP state typically a single scalar.
Why it matters Running sum is a required building block for understanding how Kadane's Algorithm (Maximum Subarray) stays correct and performant on large inputs.
What it is The maximum running sum seen across all indices visited. The answer.
Why it matters Best so far is a required building block for understanding how Kadane's Algorithm (Maximum Subarray) stays correct and performant on large inputs.
What it is At each index, choose: extend the running sum, or restart from this element. running = max(value, running + value).
Why it matters Restart decision is a required building block for understanding how Kadane's Algorithm (Maximum Subarray) stays correct and performant on large inputs.
What it is Track start/end of the winning subarray by remembering when the running sum restarts.
Why it matters Optional indexes is a required building block for understanding how Kadane's Algorithm (Maximum Subarray) stays correct and performant on large inputs.
What it is A range of consecutive indices [i..j]. Not the same as a subsequence (which can skip).
Why it matters A range of consecutive indices [i..j]. Not the same as a subsequence (which can skip). In Kadane's Algorithm (Maximum Subarray), this definition helps you reason about correctness and complexity when inputs scale.
What it is The minimal information needed to extend the solution. For Kadane: just the running sum at the previous index.
Why it matters The minimal information needed to extend the solution. For Kadane: just the running sum at the previous index. In Kadane's Algorithm (Maximum Subarray), this definition helps you reason about correctness and complexity when inputs scale.
What it is Equivalent framing: max subarray sum = max over j of (prefix[j] − min prefix[i<j]). Same complexity, different mental model.
Why it matters Equivalent framing: max subarray sum = max over j of (prefix[j] − min prefix[i<j]). Same complexity, different mental model. In Kadane's Algorithm (Maximum Subarray), this definition helps you reason about correctness and complexity when inputs scale.
What it is When every element is negative, the answer is the largest single element. Handle by initializing running = best = arr[0], not 0.
Why it matters When every element is negative, the answer is the largest single element. Handle by initializing running = best = arr[0], not 0. In Kadane's Algorithm (Maximum Subarray), this definition helps you reason about correctness and complexity when inputs scale.
Walkthrough
Every frame of the concept diagram, laid out top-to-bottom so you can scroll through the algorithm at your own pace.
Init at i=0. current = -2. best = -2.
i=1, value=1. 1 > -1 → reset window to i. current=1. best=1.
i=2, value=-3. extend window. current=-2=-2. best=1.
i=3, value=4. 4 > 2 → reset window to i. current=4. best=4.
i=4, value=-1. extend window. current=3=3. best=4.
i=5, value=2. extend window. current=5=5. best=5.
i=6, value=1. extend window. current=6=6. best=6.
i=7, value=-5. extend window. current=1=1. best=6.
i=8, value=4. extend window. current=5=5. best=6.
Tradeoffs
When this technique wins, when it loses, and what to reach for instead.
Choose this when Always prefer Kadane O(n) vs O(n²) is a free win once you understand the algorithm.
Tradeoff None of practical significance. Brute force is only easier to explain in 30 seconds.
Choose this when Kadane is faster (O(n)), simpler (no recursion), and uses constant space.
Tradeoff D&C generalizes more naturally to other problems; Kadane is a special-case shortcut.
Choose this when Kadane reads more directly. Prefix-sum is mathematically equivalent but adds an extra concept.
Tradeoff Prefix-sum framing extends naturally to 'subarray sum equals K' style problems; Kadane doesn't.
In Production
Where this shows up at real companies and what the on-call engineer learned the hard way.
Single-position 'best buy/sell window' calculations in time-series price arrays are Kadane in disguise apply it to the array of daily price differences.
Takeaway Many 'best contiguous interval' problems in finance reduce to Kadane on a transformed array.
Scoring the best 'streak' in a sequence (e.g. cumulative score with positive/negative events) is direct Kadane find the highest contiguous run.
Takeaway Streak detection in event sequences is a textbook Kadane application.
2D image filters that find the max-energy connected band run Kadane along rows or columns after a column-pair compression the 2D max-submatrix problem.
Takeaway Kadane is the inner loop of the 2D extension; understanding 1D first makes 2D obvious.
Code
A clean reference implementation in TypeScript, Python, and Go. Switch languages with the toggle.
Single linear pass. At each index, decide whether to extend or restart, and update best if the current running sum is a new max.
Complexity: Time O(n), Space O(1)
Kadane's algorithm with running and best sum
function maximumSubarraySum(values: number[]): number {
if (values.length === 0) {
throw new Error("Kadane requires at least one value.")
}
let runningSum = values[0]
let bestSumSoFar = values[0]
for (let valueIndex = 1; valueIndex < values.length; valueIndex += 1) {
const currentValue = values[valueIndex]
runningSum = Math.max(currentValue, runningSum + currentValue)
bestSumSoFar = Math.max(bestSumSoFar, runningSum)
}
return bestSumSoFar
}
Watch Out
The bugs and edge cases that bite engineers most often. Skim before you ship.
best = 0 instead of best = arr[0]. Breaks on all-negative arrays you'd return 0 for an array like [-3,-1,-2].Pro Tips
Small habits that pay off every time you reach for this pattern.
Pattern Recognition
Concrete signals that tell you this is the right pattern both at work and on LeetCode.
O(n³) total).Practice
A curated easy-to-hard problem ladder. Each one reinforces a specific aspect of the pattern.
| # | Problem | Difficulty | Typical Complexity |
|---|---|---|---|
| 1 | Best Time to Buy and Sell Stock | Easy | O(n) Kadane on price diffs |
| 2 | Maximum Subarray | Medium | O(n) |
| 3 | Maximum Sum Circular Subarray | Medium | O(n) two Kadane runs |
| 4 | Maximum Product Subarray | Medium | O(n) Kadane variant tracking min/max |
| 5 | Maximum Absolute Sum of Any Subarray | Medium | O(n) |
| 6 | K-Concatenation Maximum Sum | Medium | O(n) |
| 7 | Max Sum of Rectangle No Larger Than K | Hard | O(n³ log n) 2D Kadane variant |