Maximum Product Subarray: Why Minimum Products Become Maximums
Unlike maximum subarray sum (where Kadane’s algorithm only tracks a single running sum), multiplication with negative numbers flips signs:
- Multiplying a negative number by a negative number produces a positive number.
- Therefore, the most negative product ending at index
i - 1(curr_min) can become the most positive (maximum) product when multiplied by a negativenums[i].
To solve this in $O(n)$ time and $O(1)$ space, track both curr_max and curr_min (the maximum and minimum product of a non-empty contiguous subarray ending at the current index), along with global_max.
State Transition & Edge Cases
At each element x = nums[i], candidate products ending at i come from starting fresh at x, extending curr_max * x, or extending curr_min * x:
$$\text{candidates} = {x,; \text{curr_max} \cdot x,; \text{curr_min} \cdot x}$$
$$\text{next_max} = \max(\text{candidates}), \qquad \text{next_min} = \min(\text{candidates})$$
$$\text{global_max} = \max(\text{global_max}, \text{next_max})$$
(Note: Compute next_max and next_min together—for instance, store a temporary temp_max or compute min(x, curr_max * x, curr_min * x) and max(x, curr_max * x, curr_min * x) using the previous step’s values—so updating curr_max doesn’t corrupt curr_min.)
- Zeros (
x = 0): Bothcurr_max * 0andcurr_min * 0become0, sonext_max = max(0, ...)andnext_min = min(0, ...)cleanly reset the active product window at0, whileglobal_maxpreserves any positive product seen before the zero. - Single Negative / All Negative Arrays: Because
xitself is always included as a candidate (max(x, ...)), a single negative element or an all-negative array returns the largest (least negative) valid subarray product rather than0.
Quick Trace: nums = [2, -3, -2, 4]
- Start (
i = 0,x = 2):curr_max = 2,curr_min = 2,ans = 2. i = 1(x = -3): candidates{-3, -6, -6}$\implies$curr_max = -3,curr_min = -6,ans = 2.i = 2(x = -2): candidates{-2, 6, 12}$\implies$curr_max = 12,curr_min = -2,ans = 12(the previouscurr_min = -6times-2flipped into+12).i = 3(x = 4): candidates{4, 48, -8}$\implies$curr_max = 48,curr_min = -8,ans = 48.
Result: 48.
Code Implementation
Python Solution
def max_product_subarray(nums: list[int]) -> int:
curr_max = nums[0]
curr_min = nums[0]
global_max = nums[0]
for num in nums[1:]:
cand1 = num
cand2 = curr_max * num
cand3 = curr_min * num
curr_max = max(cand1, cand2, cand3)
curr_min = min(cand1, cand2, cand3)
global_max = max(global_max, curr_max)
return global_max
C++ Solution
#include <vector>
#include <algorithm>
int maxProduct(const std::vector<int>& nums) {
long long curr_max = nums[0];
long long curr_min = nums[0];
long long global_max = nums[0];
for (std::size_t i = 1; i < nums.size(); ++i) {
long long x = nums[i];
long long cand1 = x;
long long cand2 = curr_max * x;
long long cand3 = curr_min * x;
curr_max = std::max({cand1, cand2, cand3});
curr_min = std::min({cand1, cand2, cand3});
global_max = std::max(global_max, curr_max);
}
return static_cast<int>(global_max);
}
Complexity
- Time Complexity: $O(n)$ — single linear scan from index
1ton - 1. - Auxiliary Space: $O(1)$ — constant extra variables (
curr_max,curr_min,global_max).
