Skip to content
AI360Xpert
Beta
Difficulty: Medium1-D Dynamic Programming

Maximum Product Subarray

Problem in Plain English

Given an integer array, find the largest product of any contiguous non-empty subarray.

Problem Statement

You are given an integer array nums. A subarray takes a contiguous non-empty block.

Return the maximum product over all such blocks. Negatives flip signs, so the best block may hold an even count of them, and zeros split the array into independent parts.

Constraints

  • 1 <= nums.length <= 2 * 10^4
  • -10 <= nums[i] <= 10
  • The product of every subarray fits in a signed 32-bit integer; this includes intermediate products of contiguous blocks.

Examples

Example 1

Input
nums = [2, 3, -2, 4]
Output
6

The contiguous block [2, 3] has product 6. Including -2 makes that product negative, and the best block after it is [4] with product 4, so the overall maximum remains 6.

Example 2

Input
nums = [-2, 0, -1]
Output
0

The one-element block [0] beats both negative singletons. The two negatives cannot multiply to 2 without including the separating zero, which makes the product 0.

Example 3

Input
nums = [-3]
Output
-3

A subarray must be non-empty, so the only candidate is the lone negative value. Initializing the answer to zero would incorrectly allow an empty block.

Example 4

Input
nums = [-2, 3, -4]
Output
24

The entire block has two negatives and product 24. Before the last value, the minimum ending product is -6; multiplying it by -4 produces the maximum. Keeping only the previous maximum 3 would miss 24.

Intuition

Multiplication can reverse which partial result is useful: in [-2, 3, -4], the smallest product ending at 3 is -6, and the next -4 turns it into the largest product 24. Keep both the maximum and minimum products of non-empty subarrays ending at the current index, rather than only a global maximum. In Example 1, extending [2, 3] by -2 makes -12, so starting again at 4 beats extending that negative block, while the global answer stays 6. Zero makes both ending products zero; the next value can start a new block.

Approaches

Check Every Subarray

Solution Details

Reveal Check Every Subarray: intuition, complexity, and code

Hints

Hint 1
Sums only need the best so far, but products need the worst too, because one more negative flips worst into best.
Hint 2
At each value the new extremes come from three candidates: the value alone, or the value times either previous extreme.
Hint 3
Snapshot the previous maximum before overwriting it so the minimum update still reads the old pair.

Edge Cases

  • A lone negative like [-3] returns -3; skipping is impossible since blocks are non-empty.
  • Zeros reset both extremes; the block after a zero restarts cleanly from the value alone.
  • Two negatives such as [-2, -3] multiply into the positive answer 6.
  • A long sequence of ones or alternating -1 values is valid at maximum length and keeps products bounded.
  • A negative multiplied by a previous negative minimum can become the global best, as [-2, 3, -4] shows.

Common Mistakes and Interview Tips

  • Tracking only the maximum misses sign flips where the previous minimum times a negative is the true best.
  • Overwriting curMax before computing curMin feeds the new extreme into the old pair; use a temporary.
  • Resetting the best at zeros instead of letting the value-alone candidate handle the restart.
  • Using Python nums[1:] creates an O(n) copy even though the recurrence needs only constant space; iterate by index.
  • Assuming only full-array prefix or suffix products are bounded does not justify int arithmetic across zero-separated blocks. The actual constraint bounds every subarray product.
  • Interview tip: distinguish the best ending here from the best anywhere; the answer in Example 1 ends before the final element.

Key Takeaway

When an update can invert order, carry both extremes forward: the minimum is the future maximum one sign flip away.