Given an integer array nums, find a subarray that has the largest product, and return the product. A subarray is a contiguous non-empty sequence of elements within an array. The test cases are generated so that the answer fits in a 32-bit integer.
Example 1
Input: nums = [2,3,-2,4]
Output: 6
Explanation: The subarray [2,3] has product 6. Including -2 makes the product negative, and [4] alone is only 4.
Example 2
Input: nums = [-2,0,-1]
Output: 0
Explanation: The possible products are -2, 0, -1 and a few 0s. The largest is 0.
Example 3
Input: nums = [2,3,-2,4,-1]
Output: 48
Explanation: The two negatives cancel out, so the whole array multiplies to 2 × 3 × (-2) × 4 × (-1) = 48.
A negative number flips the sign, so today's smallest (most negative) product can become tomorrow's largest. Carry both the max and the min ending at each index.
Run checks all cases above. Submit evaluates all test cases.