Maximum Product Subarray
Track both max and min products — negatives can flip to become the max.
Problem Statement
Given an integer array nums, find a subarray that has the largest product, and return the product. The test cases are generated so that the answer will fit in a 32-bit integer.
Examples
Example 1
Explanation: [2,3] has the largest product = 6.
Example 2
Explanation: The result cannot be 2 since [-2,-1] is not a subarray.
Constraints
- 1 ≤ nums.length ≤ 2 × 10⁴
- -10 ≤ nums[i] ≤ 10
- The product of any prefix or suffix of nums fits in a 32-bit integer.
Solutions
Try all subarrays and compute their products. O(n²) or O(n³).
Related Concepts
Deepen your understanding with these related topics from our AI Glossary:
Want to master the core concepts?
Our free AI Glossary covers 190+ topics — from Dynamic Programming to Dynamic Programming, Machine Learning, SQL, and more. Structured learning tracks for every level.
Stuck? Ask AI to explain it step by step.
Ask Claude, GPT-4o, or Gemini to debug your code, generate test cases, or walk through the intuition. 39+ models. Pay only on days you use it — no subscription required.
Free to start · No credit card required to explore
Get Started Free