<p>Given an array of integers <code>nums</code>, find the maximum length of a subarray where the product of all its elements is positive.</p>

<p>A subarray of an array is a consecutive sequence of zero or more values taken out of that array.</p>

<p>Return <em>the maximum length of a subarray with positive product</em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> nums = [1,-2,-3,4]
<strong>Output:</strong> 4
<strong>Explanation:</strong> The array nums already has a positive product of 24.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> nums = [0,1,-2,-3,-4]
<strong>Output:</strong> 3
<strong>Explanation:</strong> The longest subarray with positive product is [1,-2,-3] which has a product of 6.
Notice that we cannot include 0 in the subarray since that&#39;ll make the product 0 which is not positive.</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> nums = [-1,-2,-3,0,1]
<strong>Output:</strong> 2
<strong>Explanation:</strong> The longest subarray with positive product is [-1,-2] or [-2,-3].
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
	<li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li>
</ul>

class Solution:
    def getMaxLen(self, nums: List[int]) -> int:
        """
        Product is positive if there are an even number of negative and
        no zero elements

        We can go through each run between zeros, recording the position of first negative element,
        and if we hit the last negative element and there are an odd number
        we need to leave off the first and last
        """
        def split_by_zeros(nums):
            result = []
            latest = []
            for num in nums:
                if num == 0:
                    if latest != []:
                        result.append(latest)
                    latest = []
                else:
                    latest.append(num)
            if latest != [] or result == []:
                result.append(latest)
            return result
        print(split_by_zeros(nums))
        assert split_by_zeros([0, 1, 1, 2, 0, 3, 0, 5, 8, 0]) == [[1, 1, 2], [3], [5, 8]]
                    
        def get_max_len_between_zeros(nums) -> int:
            first_neg = None
            last_neg = None
            num_negs = 0
            for i, num in enumerate(nums):
                if num < 0:
                    last_neg = i
                    if first_neg is None: first_neg = i
                    num_negs += 1
            if num_negs % 2 == 0:
                return len(nums)
            else:
                return max(
                    last_neg, # stop before last_neg
                    len(nums) - first_neg - 1 # stop after first_neg
                )
        assert get_max_len_between_zeros([1,-2,-3,4]) == 4
        

        return max(
            get_max_len_between_zeros(l) for l in split_by_zeros(nums)
        )
32299