You're given an array of integers nums, and your task is to find the maximum length of a contiguous subarray where the product of all its elements is positive.
A subarray is a consecutive sequence of elements from the original array. For example, in the array [1, -2, 3, 0], some subarrays are [1], [1, -2], [-2, 3], and [1, -2, 3].
Key insight: A product is positive when there's an even number of negative numbers in the subarray. Zero values act as "barriers" that reset our counting since any product containing zero becomes zero.
Goal: Return the length of the longest subarray with a positive product.
Example: For nums = [1, -2, -3, 4], the subarray [1, -2, -3, 4] has product 24 > 0, so the answer is 4.
Input & Output
Constraints
- 1 โค nums.length โค 105
- -109 โค nums[i] โค 109
- nums[i] can be positive, negative, or zero