IntermediateAbout 15 min
Kadane's Algorithm
Read the full article on Substack
7 min read.
Find the maximum sum contiguous subarray in O(n) time. The quintessential dynamic programming problem that every FAANG interview expects you to know.
Think of it this way: Imagine you're tracking your stock portfolio. At each day, you decide: 'Should I keep adding to my current streak, or cut losses and start fresh?' Kadane's makes this decision at every step.
Start with cur = best = a[0] = -2.
1/14
Check your understanding
Answer from memory before you scroll back up. Pulling an idea out of your head is what makes it stay there.
Kadane in one pass
Sequence
Order the steps for the maximum subarray sum.
Tap a piece, then tap where it goes — or drag it. Tap the tray to take one back.
- 1Drop a step here
- 2Drop a step here
- 3Drop a step here
- 4Drop a step here
- 5Drop a step here
5 pieces left to place
Quiz
20 questionsQuestion 1 of 20
Score: 0
What decision does Kadane's algorithm make at each element?
Done with this lesson?
Sign in free to keep your progress, streak and quiz scores.