Skip to content
Onsite

Maximum Product Subarray

MediumP2Coding

Given an integer array nums, return the largest product of any non-empty contiguous subarray.

Example 1

Input
nums = [2,3,-2,4]
Output
6
Why
The subarray [2,3] has product 6.

Example 2

Input
nums = [-2,0,-1]
Output
0
Why
The answer is 0; [-2] and [-1] are worse and no subarray with both has a positive product.

Example 3

Input
nums = [-2]
Output
-2

Constraints

  • 1 <= nums.length <= 2 * 10^4
  • -10 <= nums[i] <= 10
  • The answer fits in a 32-bit signed integer.

Hints

Run checks the 3 visible tests, Submit all 10.