Kadane's algorithm
medium 16 minFind the contiguous subarray with the largest sum in one pass.
Check yourself
One question at a time. Each answer explains why.
Question 1 of 10
For [-5, 10, -3], what happens at index 1?
cur is 5 and nums[i] is -2. What does Kadane do?
What is the maximum subarray sum of [-1, -2, -3]?
Why do we initialise cur and best with nums[0] and not 0?
What are the time and space complexity of Kadane's algorithm?
In dp[i] = max(a[i], dp[i - 1] + a[i]), what is dp[i]?
A solution returns cur after the loop instead of best. On [-2, 1, -3, 4, -1, 2, 1, -5, 4], what does it return?
What is the maximum product subarray of [2, -5, -2, -4, 3]?
On the circular array [5, -3, -2, 6, -1, 4], total = 9 and the smallest run is -5. What is the answer?
Which problem is NOT a good fit for Kadane?
Pick an answer to see why.
Not quite. max(10, -5 + 10) = max(10, 5) = 10. The running sum -5 was negative, so starting fresh at 10 is better.
Not quite. max(-2, 5 + (-2)) = max(-2, 3) = 3. cur is positive, so the run is extended even though the element is negative.
Not quite. Every element is negative, so the best non-empty subarray is the least negative single element, -1.
Not quite. A subarray must contain at least one element. Starting at 0 makes the empty subarray the answer for all-negative input.
Not quite. One pass with constant work per element, and only cur and best (plus indices) are stored.
Not quite. dp[i] is pinned to end at i, which is what makes the two-way choice complete. The answer is the maximum over all dp[i].
Not quite. cur ends at 5, the best run ending at the last index ([4, -1, 2, 1, -5, 4]). The true answer, 6, ended at index 6.
Not quite. The run [-2, -4, 3] gives 24. [2, -5, -2] gives 20 and [2, -5, -2, -4] gives -80, which is why the minimum must be tracked too.
Not quite. The wrap case is total - minRun = 9 - (-5) = 14, the run [6, -1, 4, 5]. It beats the straight best of 9.
Not quite. A fixed length k is a sliding window problem: slide a window of size k and keep its sum. Kadane is for runs of any length.
Flashcards
Say your answer, then reveal it.
Card 1 of 10
What problem does Kadane's algorithm solve?
The maximum sum of a contiguous, non-empty subarray, in O(n) time and O(1) space.
Say your answer, then reveal it.
What is the one-line update?
cur = max(nums[i], cur + nums[i]): restart at nums[i], or extend the current run. Then best = max(best, cur).
Say your answer, then reveal it.
What does cur represent?
The best sum of a subarray that ends exactly at index i. It is dp[i] in the recurrence dp[i] = max(a[i], dp[i - 1] + a[i]).
Say your answer, then reveal it.
When does Kadane restart?
Exactly when cur is negative, because nums[i] > cur + nums[i] means cur < 0. A negative element on its own does not force a restart.
Say your answer, then reveal it.
Why initialise with nums[0] rather than 0?
So an all-negative array returns its largest element. Starting at 0 returns 0, the sum of an empty subarray, which is not allowed.
Say your answer, then reveal it.
What is the answer for [-2, 1, -3, 4, -1, 2, 1, -5, 4]?
6, from [4, -1, 2, 1] at indices 3 to 6.
Say your answer, then reveal it.
How do you recover the subarray?
Set start = i on every restart; when cur beats best, copy best_l, best_r = start, i.
Say your answer, then reveal it.
How does maximum product subarray differ?
Keep hi and lo (largest and smallest product ending here), updated together from (x, hi * x, lo * x), because a negative factor swaps them.
Say your answer, then reveal it.
How do you solve the circular version?
max(maxRun, total - minRun), where minRun is Kadane with min. If maxRun < 0 (all negative), return maxRun.
Say your answer, then reveal it.
What are the brute-force costs?
O(n³) if each subarray is re-summed, O(n²) with a running sum per start. There are n(n + 1) / 2 subarrays.
Say your answer, then reveal it.
These cards come back in Today, just before you would forget them.