Learnitweb

Maximum Product Subarray

1. Problem Summary

You are given an integer array nums.
Your task is to find the contiguous subarray (containing at least one number) that has the maximum product, and return that product.

Important characteristics of this problem:

  • The subarray must be continuous.
  • The array may contain positive numbers, negative numbers, and zeros.
  • Negative numbers can flip product signs.
  • Zeros break product continuity.
  • The result must be the maximum possible product among all subarrays.

Example interpretation:
Given [-2, 3, -4], the maximum product subarray is [3, -4], whose product is -12, but the full subarray [-2, 3, -4] yields product 24, which is the maximum.


2. Key Insights

Multiplication behaves differently than addition

In max-sum problems (like Kadane’s Algorithm), only the maximum running sum matters.
But here:

  • A negative number can turn a small negative product into a large positive one.
  • A positive number may increase or decrease the running values.
  • Zero resets the product entirely.

Track both maximum and minimum at each step

Because:

  • max × negative → becomes min
  • min × negative → becomes max

So at every index, we track:

currentMax  = maximum product ending here
currentMin  = minimum product ending here

Then update them using the current number.

Global max updates at each step

We store the best product seen so far.


3. Approach

Dynamic tracking of max and min products

Steps:

  1. Initialize:
    • currentMax = nums[0]
    • currentMin = nums[0]
    • result = nums[0]
  2. For each element from index 1 onward:
    • If number is negative, swap currentMax and currentMin
    • Update: currentMax = Math.max(nums[i], currentMax * nums[i]) currentMin = Math.min(nums[i], currentMin * nums[i])
    • Update global result: result = Math.max(result, currentMax)
  3. Return result

This handles:

  • negative flipping
  • zero boundaries
  • continuous segment evaluation

4. Time and Space Complexity

Time Complexity: O(N)

Each element is processed once.

Space Complexity: O(1)

Only constant extra variables are maintained.


5. Java Solution

class Solution {
    public int maxProduct(int[] nums) {
        int currentMax = nums[0];
        int currentMin = nums[0];
        int result = nums[0];

        for (int i = 1; i < nums.length; i++) {
            int num = nums[i];

            if (num < 0) {
                int temp = currentMax;
                currentMax = currentMin;
                currentMin = temp;
            }

            currentMax = Math.max(num, currentMax * num);
            currentMin = Math.min(num, currentMin * num);

            result = Math.max(result, currentMax);
        }

        return result;
    }
}

6. Dry Run Example

Consider input:

nums = [2, 3, -2, 4]

Step-by-step tracking:

Initial:

currentMax = 2
currentMin = 2
result = 2

Index 1: num = 3

currentMax = max(3, 2×3) = 6
currentMin = min(3, 2×3) = 3
result = max(2, 6) = 6

Index 2: num = -2

num is negative → swap
currentMax = 3
currentMin = 6

currentMax = max(-2, 3×-2) = max(-2, -6) = -2
currentMin = min(-2, 6×-2) = min(-2, -12) = -12

result = max(6, -2) = 6

Index 3: num = 4

currentMax = max(4, -2×4) = 4
currentMin = min(4, -12×4) = -48
result = max(6, 4) = 6

Final answer: 6


Another Dry Run (with more negatives)

Input:

nums = [-2, 3, -4]

Initial:
currentMax = -2
currentMin = -2
result = -2

Index 1 (3):
currentMax = max(3, -2×3) = 3
currentMin = min(3, -2×3) = -6
result = 3

Index 2 (-4), negative ⇒ swap first
currentMax = -6
currentMin = 3

currentMax = max(-4, -6×-4) = max(-4, 24) = 24
currentMin = min(-4, 3×-4) = -12

result = max(3, 24) = 24

Final answer: 24