Maximum Subarray
Let cur be the largest sum of a run that ends at the current index. At each index, the best run ending here is either this number alone, or this number plus cur, whichever is bigger. Keep best as the largest cur seen so far.
cur is always the best sum of a subarray that ends exactly at the current index, so it is a valid candidate for the answer at every step.
Problem
You are given an integer array nums. Find a contiguous part of it, a subarray with at least one number, whose sum is as large as possible. Return that largest sum.
A subarray is a run of neighbouring numbers. You cannot skip numbers in the middle.
An integer array nums of length n.
One number: the largest sum of any non-empty subarray.
The run [4, -1, 2, 1] adds up to 6. No other run of neighbours does better.
The only subarray is [1].
The whole array is the best run: 5 + 4 − 1 + 7 + 8 = 23.
- • 1 ≤ nums.length ≤ 10^5
- • −10^4 ≤ nums[i] ≤ 10^4
First, think yourself
Before opening any solution, try to answer these on paper. Use hints only when stuck — each hint is counted.
- 1.
The answer is a run of neighbours. If I know the best run that ends at index 4, how can it help me at index 5?
- 2.
Can the best run be empty? What is the answer for an array with only one negative number?
- 3.
If the run so far has a negative sum, is it worth keeping before I add the next number?
- 4.
Must the best run end at the last index? Where else could it end?
- 5.
How many runs end at one particular index?
Brute force approach
Better approach
Optimized approach
Why does this algorithm work?
Dry run
All approaches compared
| Approach | Idea | Time | Space |
|---|---|---|---|
| Brute Force | Add up every subarray from scratch and keep the largest sum | O(N^3) | O(1) |
| Better | Fix the start, grow the end one step at a time, keep one running sum | O(N^2) | O(1) |
| Optimal | Track the best run that ends at each index: extend it or restart | O(N) | O(1) |
Why does this belong to this pattern?
- “contiguous subarray”The answer is a run of neighbours. Think of the best run that ends at each index.
- “largest sum”A sum can be updated in O(1): add the new number, or drop the old run and start again.
- “at least one number”The answer can be negative, so the start value is a real number, never 0.
This is the anchor of the Maximum subarray sub-pattern. The length of the run is free, so the only decision at each index is extend or restart. The next three questions keep this rule and change the comparison: the smallest sum, the absolute sum, and one deletion.
Don't make this mistake
Resetting to 0 lets cur become an empty run. On the dry run [-2, 1, -3, 4, -1, 2, 1, -5, 4] it still prints 6, so the bug hides. But at index 2 it sets cur = 0, while the correct run ending there is −2. On [-3, -1, -2] it prints 0, but the answer is −1, the largest single number.
This picks numbers from anywhere, so it finds a subsequence, not a subarray. On the dry run it gives 1 + 4 + 2 + 1 + 4 = 12, which skips the −3, −1 and −5 in between. The answer must be one run of neighbours, which is 6.
The best run can end in the middle of the array. On the dry run the last cur is 5, for the run [4, −1, 2, 1, −5, 4]. The best run, [4, −1, 2, 1], ends at index 6 and sums to 6.
Want to implement this?
Write it yourself on LeetCode, then come back and compare your code with the solutions above.
Open coding workspaceHow this concept connects to System Design & C++ Systems
Top engineering interviews test whether you understand how algorithms like DP — Kadane / Subarray translate into production architectures and low-level memory mechanics.
Foundations: Object-Oriented Principles in C++23
Class invariants, pure virtual interfaces, and RAII smart pointers.
Food Delivery System (Zomato Architecture)
Real-world machine coding application of algorithms and design patterns.
Pointers vs References Deep Dive
Pointer arithmetic and memory layout under the hood.
Virtual Method Table (vtable) & Memory Layout
Polymorphism, vptr overhead, and multiple inheritance in memory.