LeetCode 53 · Medium
Maximum Subarray Explained: Kadane's Algorithm (LeetCode 53)
Updated Nov 27, 2026 · 3 min read
Walk the array once with a running total. At each number, either add it to the total or start fresh from it, whichever is bigger, and remember the best total you've seen. That's Kadane's algorithm: O(n) time, O(1) space, and it drops any running total that has gone negative.
Key points
- ●Checking every start and end is O(n²): 28 runs for 7 numbers, about half a million for 1,000.
- ●A negative running total only drags the next number down, so drop it.
- ●current = max(num, current + num), best = max(best, current).
- ●Start both from nums[0], not 0, so an all-negative list still works.

The riddle: Maya's best streak
Maya runs a stand and writes down seven days of profit and loss: 3, -4, 5, -1, 2, -6, 4. What's the best total she made over days in a row?
That's LeetCode 53, Maximum Subarray. You get a list of whole numbers, some negative, and you return the largest sum of a run of neighbours. The run has to hold at least one number.
For Maya the answer is 6, from the run 5, -1, 2.
The slow way: try every start and every end
The obvious approach picks a start day, picks an end day, and adds up everything between them. Seven days have 28 possible runs. A thousand days have about half a million. That's O(n²) even if you're clever about the sums, and O(n³) if you re-add each range from scratch. The problem allows 100,000 numbers, so it's too slow.
The idea: debt only drags you down
Instead of asking "which run is best?", ask "what's the best run that ends today?"
There are only two choices for a run ending at today's number. You either extend yesterday's best run, or you start fresh from today. If yesterday's running total is negative, it's debt. Carrying it forward can only make today's sum smaller, so you drop it and start over.
That gives two lines:
current = max(num, current + num), the best run ending herebest = max(best, current), the best run anywhere so far
This is Kadane's algorithm.
Why is dropping safe? Any run that ends today and includes a negative-sum prefix would be bigger without that prefix. So the best run ending today never carries a negative total from yesterday. You don't lose a winner by throwing it away, and best keeps whatever you've already found.
Trace it on Maya's days
| Day's number | current | best |
|---|---|---|
| 3 | 3 | 3 |
| -4 | -1 | 3 |
| 5 | 5 | 5 |
| -1 | 4 | 5 |
| 2 | 6 | 6 |
| -6 | 0 | 6 |
| 4 | 4 | 6 |
On day 2 the total falls to -1. Day 3's 5 is better on its own than -1 plus 5, so the run restarts at 5. It climbs to 6 on day 5, and nothing after that beats it. Step 5 of the visual sheet above shows the same trace, and step 3 has the extend-or-restart picture.
The code in Python
def max_sub_array(nums):
current = best = nums[0]
for num in nums[1:]:
current = max(num, current + num)
best = max(best, current)
return best
Run it on [3, -4, 5, -1, 2, -6, 4] and it returns 6. On LeetCode's own first example it returns 6 as well, from the run 4, -1, 2, 1.
Complexity
- Time: O(n), one pass over the list.
- Space: O(1), two numbers.
You never store a subarray, only its sum, and you go from O(n²) to O(n).
The trap to watch for
Starting best or current at 0. It feels natural, since an empty total is 0, but the run can't be empty. With [-8, -3, -5] a zero start returns 0, and the correct answer is -3. Starting both values from nums[0] fixes it, which is why step 7 of the sheet tests an all-negative list.
The other mix-up is returning current at the end. current is the best run ending at the last number, which for Maya is 4. You want best, which remembers the 6 from earlier.
Frequently asked questions
What is Kadane's algorithm?
It's the one-pass method for Maximum Subarray. You keep the best sum of a run that ends at the current number, and the best sum seen anywhere. At each number you either extend the run or restart from that number, whichever gives more.
What is the time complexity of Maximum Subarray?
Kadane's algorithm is O(n) time and O(1) space. Trying every start and end is O(n²) if you keep a running sum, and O(n³) if you re-add each range from scratch.
Why not start the answer at 0?
The subarray can't be empty. If every number is negative, like [-8, -3, -5], the right answer is -3. Starting at 0 would return 0, which isn't the sum of any real run.
How is Maximum Subarray different from Maximum Product Subarray?
With sums you track one running value. With products a negative number can turn the smallest product into the largest, so LeetCode 152 tracks both a running biggest and a running smallest.