<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 {
public:
    int getMaxLen(vector<int>& nums) {
        ios_base::sync_with_stdio(false);
        cin.tie(nullptr);
        cout.tie(nullptr);
        vector<int> x;
        x.push_back(0);
        for (auto i:nums) x.push_back(i);
        x.push_back(0);
        int id0[50000],cb[50000],ca[50000],nn[50000],n0=0,n=nums.size()+2,c=0;
        for (int i=0;i<n;i++){
            if (x[i]==0) {
                if (n0!=0) nn[n0-1]=c;
                ca[n0]=-1;
                id0[n0++]=i;
                c=0;
            }
            if (x[i]<0) c++;
            if (x[i]<0 && ca[n0-1]==-1) ca[n0-1]=i;
        }
        int k=n0-1;
        for (int i=n-1;i>=0;i--){
            if (x[i]==0){
                cb[k]=-1;
                k--;
            }
            if (x[i]<0 && cb[k+1]==-1) cb[k+1]=i;
        }
        cb[0]=-1;
        ca[n0-1]=-1;
        int max=0;
        for (int i=0;i<n0-1;i++){
            int l,r;
            if (nn[i]%2){
                if (ca[i]-id0[i]>id0[i+1]-cb[i+1]){
                    l=id0[i]+1;
                    r=cb[i+1]-1;
                }else{
                    l=ca[i]+1;
                    r=id0[i+1]-1;
                }
            }else{
                l=id0[i]+1;
                r=id0[i+1]-1;
            }
            if (max<r-l+1) max=r-l+1;
        }
        return max;
    }
};
66300