Problem: Maximum Product Subarray
Given an integer array nums, find the contiguous subarray within an array (containing at least one number) which has the largest product, and return the product.
Example:Input:
nums = [2,3,-2,4]Output:
6Explanation:
The subarray [2,3] has the largest product 6.
Constraints:
1 <= nums.length <= 2 * 10^4
-10 <= nums[i] <= 10
The product of any prefix or suffix of nums fits in a 32-bit integer.
This problem is similar in spirit to Maximum Subarray, but multiplication introduces additional complexity because a negative number can flip the sign of the product. At each step, you must track both the maximum and minimum product ending at the current position, since a previous minimum (negative) multiplied by a negative number can become the new maximum. By maintaining these two values while scanning the array, you can compute the global maximum product in linear time.