Introduction
The Maximum Product Subarray problem involves finding the contiguous subarray that has the largest product.
Given an integer array arr[], the task is to find:
- the contiguous subarray having maximum product
- and return that product
Unlike Maximum Subarray Sum:
- multiplication with negative numbers can completely change the result
This problem helps in understanding:
- product-based traversal
- handling negative numbers
- dynamic tracking
- optimization techniques
Example
Input: arr[] = [2,3,-2,4]Output: 6
Explanation: Subarray: [2,3] Product = 6
This is the maximum product subarray.
Input: arr[] = [-2,0,-1]
Output: 0
Explanation:
Possible products:
[-2] = -2
[0] = 0
[-1] = -1
Maximum Product = 0
Constraints
1 <= n <= 2 * 10^4-10 <= arr[i] <= 10
Approach 1 : Brute Force
Explanation
The simplest way to solve this problem is:
- Generate all possible subarrays
- Calculate the product of every subarray
- Track maximum product
This approach is easy to understand but repeated product calculations increase time complexity.
Steps
- Traverse all starting indices.
- Generate subarrays from every index.
- Calculate current product.
- Update maximum product.
- Return final answer.
Dry Run
Input Array:[2,3,-2,4]
Start from 2: Current Product = 2 Maximum Product = 2
Multiply by 3: Current Product = 6 Maximum Product = 6
Multiply by -2: Current Product = -12
Multiply by 4: Current Product = -48
Continue traversal...
Final Result: 6
Brute Force Code
Complexity Analysis
Time Complexity: O(n²)
Explanation:
All possible subarrays are generated and processed.
Space Complexity: O(1)
Explanation:
No extra data structures are used.Approach 2 : Optimized Solution
Explanation
The optimized solution keeps track of:
- Maximum product till current index
- Minimum product till current index
Why minimum product?
Because:
- multiplying two negative numbers can produce a large positive product
So:
- minimum product can suddenly become maximum
Steps
- Initialize:
- max_product
- min_product
- answer
- Traverse the array.
- If current number is negative:
- swap max and min products
- Update:
- current maximum product
- current minimum product
- Update final answer.
Dry Run
Input Array:[2,3,-2,4]Initially: max_product = 2
min_product = 2
answer = 2
Traverse 3: max_product = 6
min_product = 3 answer = 6
Traverse -2: Negative number found Swap max and min
max_product = -2
min_product = -12
answer = 6
Traverse 4: max_product = 4
min_product = -48
Final Result: 6
Optimized Code
Complexity Analysis
Complexity Analysis
Time Complexity: O(n)
Explanation: Each element is processed once.Space Complexity: O(1)
Explanation:
Only variables are used for tracking products.
Edge Cases
- Array contains zeros
- Array contains all negative numbers
- Array contains one element
- Product becomes very large
- Maximum product contains negative numbers
Why This Problem is Important
This problem helps in understanding:
- Product-based traversal
- Handling negative numbers
- Dynamic tracking
- Greedy optimization
- State maintenance
It is one of the most important advanced array interview problems.
Real-World Applications
Product optimization concepts are used in:
- Financial growth analysis
- Statistical modeling
- Signal processing
- Data trend analysis
- Performance analytics
Common Mistakes
- Forgetting minimum product tracking
- Incorrect swap logic
- Ignoring negative numbers
- Resetting products incorrectly
Interview Tips
Interviewers often expect:
- O(n) optimized solution
- Proper negative number handling
- Maximum and minimum tracking explanation
Always explain why minimum product is necessary.
Related Questions
- Maximum Subarray
- Circular Subarray Sum
- Product Except Self
- Kadane’s Algorithm
- Maximum Sum Rectangle
Final Takeaway
The Maximum Product Subarray problem is a fundamental optimization problem that teaches product-based traversal and handling negative number behavior efficiently. Understanding this problem builds a strong foundation for advanced dynamic programming and array optimization interview problems.