Given an integer array nums, find the contiguous subarray (containing at least one element) that has the largest product, and return that product.
This is a twist on the classic Maximum Subarray problem! Unlike sum where we only worry about negative numbers, with products we need to consider both negative numbers (which can flip signs) and zeros (which reset everything).
Key Challenge: A negative number can turn a small product into a large one if multiplied by another negative number!
Example: In array [2,3,-2,4], the maximum product subarray is [2,3] with product 6, not the entire array which gives -48.
Input & Output
Constraints
- 1 โค nums.length โค 2 ร 104
- -10 โค nums[i] โค 10
- The product of any prefix or suffix of nums is guaranteed to fit in a 32-bit integer