LeetCode 152 · Medium

Maximum Product Subarray Explained (LeetCode 152)

Updated Nov 27, 2026 · 3 min read

Walk the array once and keep two running products: the biggest and the smallest that end at the current number. A negative number swaps them, because multiplying the smallest (most negative) product by a negative can make the new biggest. The answer is the largest 'biggest' you see, in O(n) time and O(1) space.

Watch onYouTubeInstagram

Key points

  • ●Checking every subarray is O(n²): 200 million products for 20,000 numbers.
  • ●A negative number flips biggest and smallest, so you must track both.
  • ●Each step, the new biggest is the max of x, x times biggest, x times smallest.
  • ●One pass, two variables: O(n) time, O(1) space.
Step-by-step visual sheet: Maximum Product Subarray
The full visual sheet, made with Edithly.Open the interactive sheet

The riddle: two minus cards won Leo sixty dollars

Leo is on a game show with four cards in a row: 2, -5, -2, 3. He picks a run of cards that sit side by side and multiplies them. The biggest product wins.

He picks all four. 2 times -5 is -10, times -2 is 20, times 3 is 60. The two minus cards cancel each other out, and that is the whole trick of LeetCode 152, Maximum Product Subarray: given an integer array, return the largest product of any contiguous subarray.

The slow way: try every run

The obvious approach tries every start and every end and multiplies what is between them. Four cards have 10 runs. 20,000 numbers, the problem's limit, have about 200 million. That is O(n²), and it times out.

The idea: a minus sign flips the order

With sums, a bigger running total is always better, which is why Kadane's algorithm keeps one number. With products, a negative number turns things upside down: the most negative product so far becomes the biggest the moment you multiply it by another negative.

So Leo keeps two numbers as he walks the row:

  • big: the largest product of a run that ends at the current card
  • small: the smallest product of a run that ends at the current card

At every card x, the new run either starts fresh at x, or extends the old biggest, or extends the old smallest:

  • new big = max(x, x × big, x × small)
  • new small = min(x, x × big, x × small)

Trace it on Leo's cards

Card big small best so far
2 2 2 2
-5 -5 -10 2
-2 20 -2 20
3 60 -6 60

At -2, the smallest product (-10) times -2 becomes 20 and jumps to the top. Step 5 of the visual sheet above traces the same four cards box by box.

The code in Python

def maxProduct(nums):
    big = small = best = nums[0]
    for x in nums[1:]:
        candidates = (x, x * big, x * small)
        big, small = max(candidates), min(candidates)
        best = max(best, big)
    return best

Compute both from the old values in one line, as above. Updating big first and then using the new big to compute small is the most common bug.

Complexity

  • Time: O(n), one pass over the array.
  • Space: O(1), three variables.

From O(n²) to O(n) by remembering one extra number.

The trap to watch for

Zeros and single negatives. With [-2, 0, -1] the answer is 0, not 2, because -2 and -1 are not next to each other. The three-candidate rule handles it: at 0 both products reset to 0, and x on its own restarts the run.

Frequently asked questions

Why do you need the minimum product in Maximum Product Subarray?

Because a negative number turns the smallest product into the largest. With [2, -5, -2], the smallest product ending at -5 is -10, and -10 times -2 is 20, the best answer so far. If you only kept the maximum you would miss it.

Is Maximum Product Subarray the same as Kadane's algorithm?

It is Kadane's idea extended to products. Kadane keeps one running best for sums; here you keep a running best and a running worst, because multiplying by a negative swaps them.

What happens when the array has a zero?

A zero resets both running products to 0, and the next number starts a new run, since x on its own is one of the three candidates. The answer can still be 0 if every other run is negative.

What is the time complexity of the optimal solution?

O(n) time, because you look at each number once, and O(1) space, because you only store the biggest, the smallest and the best answer.

Turn any problem or document into a visual sheet like this one

Paste a LeetCode problem, a PDF or a link into Edithly and get a step-by-step sheet you can study, share or quiz yourself on.

Make this sheet in Edithly, free