Maximum Product Subarray

Given an integer array nums, return the largest product of any non-empty contiguous subarray. Pattern focus: Max product subarray. Track both the maximum and minimum product ending at each position because a negative number can flip the sign.

Input Format

nums = integer array

Output Format

maximum product of any contiguous subarray

Constraints

  • 1 <= nums.length <= 10^5; -10 <= nums[i] <= 10

Examples

Example 1:

Input:

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

Output:

6

Explanation:

The best subarray is [2,3].

Example 2:

Input:

nums = [-2,0,-1]

Output:

0

Explanation:

The subarray [0] gives the highest product.

Loading...
Maximum Product Subarray - Dp 1d DSA Problem