Solvarch
DSA ProblemsProgressPlanRoadmapsReviewCheat SheetsCompaniesSystem DesignCS FundamentalsQuestion Bank
Theme
Problems

Maximum Product Subarray

Medium
arraydynamic-programming

Given an integer array nums, find a contiguous non-empty subarray with the largest product, and return that product.

Input: one line of space-separated integers. Output: the maximum product.

Example 1
Input:
2 3 -2 4
Output:
6

[2,3] gives 6.

Example 2
Input:
-2 0 -1
Output:
0

The best you can do is 0.

Constraints

  • 1 ≤ nums.length ≤ 2·10^4
  • -10 ≤ nums[i] ≤ 10
  • The answer fits in a 32-bit integer.
Watch a video walkthrough

Run your code against the sample tests, or Submit to run the full suite.