 All Problems
Maximum Product Subarray
medium
arrays
dynamic programming
amazon
linkedin
microsoft

Given an integer array nums, find the contiguous subarray with the largest product and return it.

Example 1:

Input: 2 3 -2 4
Output: 6
Explanation: [2,3] has the largest product 6.

Example 2:

Input: -2 0 -1
Output: 0

Constraints:

  • 1 ≤ nums.length ≤ 2 × 10⁴
  • -10 ≤ nums[i] ≤ 10

Input format: Space-separated integers.

Output format: Maximum product.

Run to check your code against the sample cases, or submit to run every case