Arrays
Kadane's Algorithm
Find the maximum sum contiguous subarray in O(N) linear time and O(1) space using dynamic programming local vs global optimization.
Last Updated: August 2, 2026
•
20 min read
1. Introduction
What is Kadane's Algorithm?
Kadane's Algorithm is an elegant dynamic programming algorithm used to find the maximum sum of a contiguous subarray within a 1D array of numbers.Why is it Important?
A brute-force solution checking allO(N²) contiguous subarrays takes O(N³) or O(N²) time. Kadane's algorithm solves this in a single pass taking O(N) linear time and O(1) space complexity.
Where is it Used?
2. Mental Model
Imagine walking along a path collecting coins (+ numbers) and paying toll fees (- numbers).
At each step, you ask yourself:
"Is my current accumulated money actually helping me, or is it negative baggage that hurts my future total?"
If your accumulated balance drops below zero, throw it away! Reset your wallet to 0 and start collecting fresh from the current position. Carrying negative debt into future steps only reduces your potential maximum sum.
3. Concept: The Core Recurrence Relation
At index i, the maximum sum of a subarray ending at index i is:
currentMax[i] = \max(nums[i], \, currentMax[i - 1] + nums[i])
globalMax = \max(globalMax, \, currentMax[i])
Maximum Circular Subarray Sum Variant
4. Visuals
Step-by-Step Trace Matrix for [-2, 1, -3, 4, -1, 2, 1, -5, 4]
Trace Table
| Index i | nums[i] | Choice: max(nums[i], cur + nums[i]) | currentMax | globalMax |
|---|---|---|---|---|
| 0 | -2 | Start baseline | -2 | -2 |
| 1 | 1 | \max(1, -2 + 1) = 1 | 1 (Reset) | 1 |
| 2 | -3 | \max(-3, 1 - 3) = -2 | -2 | 1 |
| 3 | 4 | \max(4, -2 + 4) = 4 | 4 (Reset) | 4 |
| 4 | -1 | \max(-1, 4 - 1) = 3 | 3 | 4 |
| 5 | 2 | \max(2, 3 + 2) = 5 | 5 | 5 |
| 6 | 1 | \max(1, 5 + 1) = 6 | 6 | 6 |
| 7 | -5 | \max(-5, 6 - 5) = 1 | 1 | 6 |
| 8 | 4 | \max(4, 1 + 4) = 5 | 5 | 6 |
5. Real-World Examples
6. Interview Perspective
How Interviewers Ask This Topic
Interviewers often ask: "Find the contiguous subarray with the largest sum and return its sum (or its actual subarray elements)."Common Mistakes
Warning: 1. Initializing
globalMax = 0: If all elements in the input array are negative (e.g., [-5, -2, -8]), initializing globalMax = 0 returns 0 instead of the correct answer -2. Always initialize globalMax = nums[0].> 2. Confusing Subarray with Subsequence: Subarrays MUST be contiguous. If non-contiguous elements are allowed, it's a Subsequence problem (solved via standard DP or greedy sorting).
7. Summary
cur = max(num, cur + num).currentMax = nums[0] and globalMax = nums[0] to handle negative arrays.O(N) time and O(1) auxiliary space.8. Quiz
Question 1: What does Kadane's algorithm return for the array [-5, -3, -8, -1, -4]?
Answer:-1 (the maximum single negative element).
Question 2: What condition causes Kadane's algorithm to discard the current running sum and start fresh?
Answer: WhencurrentMax + nums[i] < nums[i], which happens whenever currentMax < 0 (the previous accumulated sum was negative).
Question 3: How do you solve Maximum Sum Subarray in a CIRCULAR array?
Answer: The answer is\max(Kadane(nums), \, TotalSum - MinimumSubarraySum(nums)), provided not all numbers are negative.
Question 4: What is the space complexity of Kadane's Algorithm?
Answer:O(1) space because it only uses two scalar variables (currentMax and globalMax).
Question 5: Can Kadane's Algorithm be extended to 2D matrices (Maximum Sum Submatrix)?
Answer: Yes! By fixing top and bottom row boundaries and using 1D Kadane on column sums, a 2D submatrix max sum can be found inO(Rows² × Cols) time.