Back to DSA sheet

Maximum Product Subarray

MediumArrays
Open on LeetCodeAmazonGoogleMicrosoft

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

Input format (stdin): the first line has n. The second line has n integers. Output the maximum product.

Examples
Input: 4 2 3 -2 4
Output: 6
[2,3] has product 6.
Input: 3 -2 0 -1
Output: 0
The best product is 0.
Constraints
  • 1 <= n <= 2*10^4
  • -10 <= nums[i] <= 10
  • answer fits in 32 bits
Sheets
Blind 75Love Babbar 450NeetCode 150NeetCode 250Striver A2Z
maximum-product-subarray.cpp3 sample tests
Loading editor
Test results

Run the sample tests to check your solution against expected output.

Custom input (stdin)
Output

Run your code to see its output.