48. Maximum Product Subarray in an Array

Given an integer array nums. Find the subarray with the largest product, and return the product of the elements present in that subarray.

A subarray is a contiguous non-empty sequence of elements within an array.

Example 1:

Input: nums = [4, 5, 3, 7, 1, 2]

Output: 840

Explanation:

The largest product is given by the whole array itself

Example 2:

Input: nums = [-5, 0, -2]

Output: 0

Explanation:

The largest product is achieved with the following subarrays [0], [-5, 0], [0, -2], [-5, 0, -2].

Now Your Turn!

Pick the correct output for the given input

Input: nums = [1, -2, 3, 4, -4, -3]

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • 1 <= nums.length <= 104
  • -10 <= nums[i] <= 10
  • -109 <= product of any prefix or suffix of nums <= 109

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

0
class Solution {
public:
int maxProduct(vector<int>& nums) {
}
};
Test Case

Input:

Nums