Largest Product of Any Contiguous Subarray
Solve this Problemnums that may contain negative numbers and zeros, find the largest product achievable by any non-empty contiguous subarray.
The tricky part is negatives: a very negative running product can flip into the best possible product the moment it's multiplied by another negative. Plain Kadane's algorithm only tracks a running maximum — here you need to also track a running minimum, since today's worst product might become tomorrow's best one. Whenever the next number is negative, swap the running max and min before extending them — that's what makes the O(n) Kadane's AlgorithmKadane's AlgorithmA single-pass dynamic programming technique that extends or restarts a running best-so-far value at each position, avoiding the need to re-examine every subarray from scratch. variant work for products instead of just sums.
Test Case 1:
Test Case 2:
Test Case 3:
Constraints
- ◆
1 ≤ nums.length ≤ 2×10⁴ - ◆
-10 ≤ nums[i] ≤ 10
Try the Dry Run
Don't just read the solution — watch it execute, one step at a time.
| 1 | class Solution { |
| 2 | public int maxProductSubarray(int[] nums) { |
| 3 | int maxProduct = nums[0]; |
| 4 | int curMax = nums[0], curMin = nums[0]; |
| 5 | for (int i = 1; i < nums.length; i++) { |
| 6 | int num = nums[i]; |
| 7 | if (num < 0) { |
| 8 | int temp = curMax; |
| 9 | curMax = curMin; |
| 10 | curMin = temp; |
| 11 | } |
| 12 | curMax = Math.max(num, curMax * num); |
| 13 | curMin = Math.min(num, curMin * num); |
| 14 | maxProduct = Math.max(maxProduct, curMax); |
| 15 | } |
| 16 | return maxProduct; |
| 17 | } |
| 18 | } |
| 19 |
-2-2-2Start curMax, curMin, and maxProduct all at nums[0] = -2.
Approach & Solutions
Brute Force
BruteFor every possible starting index, multiply forward one element at a time and keep the best product seen. Correct, but re-multiplying from scratch for every subarray wastes the fact that consecutive subarrays share almost all of their elements.
O(n²)O(1)1class Solution {
2 public int maxProductSubarray(int[] nums) {
3 int maxProduct = nums[0];
4 for (int i = 0; i < nums.length; i++) {
5 int product = 1;
6 for (int j = i; j < nums.length; j++) {
7 product *= nums[j];
8 maxProduct = Math.max(maxProduct, product);
9 }
10 }
11 return maxProduct;
12 }
13}Optimal — Kadane's Algorithm (Track Max and Min)
OptimalA variant of Kadane's algorithm adapted for multiplication. Unlike sums, a very negative running product can become the best product if multiplied by another negative — so track both a running max and a running min ending at each position. Whenever the current number is negative, swap max and min first, since multiplying by a negative flips which one is more extreme.
O(n)O(1)1class Solution {
2 public int maxProductSubarray(int[] nums) {
3 int maxProduct = nums[0];
4 int curMax = nums[0], curMin = nums[0];
5 for (int i = 1; i < nums.length; i++) {
6 int num = nums[i];
7 if (num < 0) {
8 int temp = curMax;
9 curMax = curMin;
10 curMin = temp;
11 }
12 curMax = Math.max(num, curMax * num);
13 curMin = Math.min(num, curMin * num);
14 maxProduct = Math.max(maxProduct, curMax);
15 }
16 return maxProduct;
17 }
18}