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.

    cur -2-2011-3243-142516-5748i

    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.

    1. 1Drop a step here
    2. 2Drop a step here
    3. 3Drop a step here
    4. 4Drop a step here
    5. 5Drop a step here
    5 pieces left to place

    Quiz

    20 questions
    Question 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.

    Keep going