Blind 75
56 / 75Free practice

Maximum Product Subarray

Medium · Dynamic programming

Jump to code

Find the largest product of any nonempty contiguous subarray of a nonempty integer array. Values may be positive, negative, or zero.

Examples

Example 1

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

Output: 6

Example 2

nums = [-2,0,-1]

Output: 0

Constraints

  • 1 <= nums.length <= 1000
  • -10 <= nums[i] <= 10
  • The result fits a signed 32-bit integer.
Need a hint?

A very negative product becomes useful when multiplied by another negative value.

Asked in real interviews

Candidates reported versions of Maximum Product Subarray from these company interviews. Some company questions are locked and open with Premium or XP.

Browse all real coding interview questions

Accepted submissions mark this problem complete. Progress stays in this browser.

Run checks 5 examples. Submit checks all 8 cases. A free account runs this problem; no XP needed.
Language
Case 1
Input
[
  [
    2,
    3,
    -2,
    4
  ]
]
Expected
6
Case 2
Input
[
  [
    -2,
    0,
    -1
  ]
]
Expected
0
Case 3
Input
[
  [
    0,
    2
  ]
]
Expected
2
Case 4
Input
[
  [
    -2,
    -3,
    7
  ]
]
Expected
42
Case 5
Input
[
  [
    -2,
    -3,
    -4
  ]
]
Expected
12

Drafts are saved per language in this browser when storage is available.