Dynamic programming aspect in Kadane's algorithm
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
Kadane's algorithm is often presented as a clever running-sum trick, but its real foundation is dynamic programming. At each position, it solves a tiny subproblem: what is the maximum subarray sum that ends exactly here? Once you see that recurrence, the algorithm becomes much easier to reason about and to modify.
The Dynamic Programming Recurrence
Suppose best_end[i] means the maximum subarray sum that must end at index i. Then there are only two possibilities for the best subarray ending at i:
- start fresh with
arr[i] - extend the best subarray ending at
i - 1
That gives the recurrence:
The global answer is then the maximum value of best_end[i] over the whole array.
This is classic dynamic programming:
- the problem has optimal substructure
- each state depends on the previous state
- you can compute the answer in one pass
A DP Array Version
Writing it with an explicit DP array makes the idea obvious.
This prints 6, because the best subarray is [4, -1, 2, 1].
The DP array is useful for learning and debugging, because you can inspect the value of every subproblem.
Why Kadane's Algorithm Uses Only Two Variables
Once you notice that best_end[i] depends only on best_end[i - 1], the full DP array becomes unnecessary. You can keep only the current state and the best answer seen so far.
This is still dynamic programming. It is just the space-optimized form of the same recurrence.
That is the key point people sometimes miss: Kadane's algorithm is not a different idea from DP. It is DP compressed into constant memory.
Recovering The Actual Subarray
The basic algorithm gives only the sum, but you can extend it to track the start and end indices.
This is another reason the DP viewpoint helps. Once you understand the state transition, extending the algorithm becomes straightforward.
Common Pitfalls
The most common mistake is initializing the running sums to 0. That breaks the all-negative case, because the correct answer for [-4, -2, -7] is -2, not 0.
Another issue is treating Kadane's algorithm as a magic rule without understanding the recurrence. That makes it harder to adapt the algorithm for related problems such as tracking indices or adding constraints.
It is also easy to forget the distinction between current_max, which is the best subarray ending at the current position, and global_max, which is the best seen anywhere so far.
Finally, the standard form solves the non-empty maximum subarray problem. If your specification allows an empty subarray with sum 0, the initialization rules change.
Summary
- Kadane's algorithm is a dynamic programming solution to the maximum subarray problem.
- The core recurrence is
max(arr[i], arr[i] + best_end[i - 1]). - The usual two-variable implementation is just the space-optimized form of that DP.
- Understanding the DP state makes it easier to recover indices or extend the algorithm.
- Initialize carefully so arrays containing only negative numbers are handled correctly.
Related reading
- Dynamic programming Code Wars twice linear algorithm times out
- Dynamic Programming Coin Change Problems
- Dynamic Programming Sum-of-products
- Dynamic Programming Why the need for optimal sub structure
- Dynamic quantization in Pytorch starts random training after quantization
- Dynamically updating shortest paths
- Dynamically add new queues, bindings and exchanges as beans
- Dynamically changing the instanceindex with spring cloud stream kafka

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.