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

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

Return the maximum length of a subarray with positive product.

&nbsp;
Example 1:

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


Example 2:

Input: nums = [0,1,-2,-3,-4]
Output: 3
Explanation: 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'll make the product 0 which is not positive.

Example 3:

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


&nbsp;
Constraints:


	1 &lt;= nums.length &lt;= 105
	-109 &lt;= nums[i] &lt;= 109


class Solution:
    def getMaxLen(self, arr: List[int]) -> int:
        n=len(arr)
        def solve(nums):
            i,j,last_neg,neg,ans=0,0,None,0,0
            while j<n:
                while j<n and nums[j]!=0:
                    if nums[j]<0: 
                        neg+=1
                        last_neg=j
                    j+=1
                if neg%2==0:
                    ans=max(ans,j-i)
                elif last_neg!=None:
                    ans=max(ans,last_neg-i,j-last_neg-1)
                i,j,neg,last_neg=j+1,j+1,0,None
            return ans
        return max(solve(arr),solve(arr[::-1]))

        
            
            
class Solution {
    public int getMaxLen(int[] nums)
    {
        int first_negative=-1;
        int zero_position=-1;
        int count_neg=0;
        int res=0;
        for(int i=0;i<nums.length;i++)
        {
            if (nums[i]<0)
            {
                count_neg = count_neg+1;
                if(first_negative==-1)
                {
                    first_negative=i;
                }
            }
            if (nums[i]==0)
            {
                count_neg = 0;
                zero_position=i;
                first_negative=-1;
            }
            else
            {
                if (count_neg%2==0)
                {
                    res=Math.max(i-zero_position,res);
                }
                else
                {
                    res=Math.max(i-first_negative,res);
                }
            }
        }
        return res;
    }
}
class Solution {
public:
    int getMaxLen(vector<int>& nums) {
        int ans = 0;
        int lprod = 1,rprod = 1;
        int llen = 0, rlen = 0;
        int n = nums.size();
        for(int i = 0 ; i < n ; i++){
            lprod *= nums[i] != 0 ? nums[i]/abs(nums[i]) : 0;
            rprod *= nums[n-1-i] != 0 ? nums[n-1-i]/abs(nums[n-1-i]) : 0;
            if(lprod != 0) llen ++;
            if(rprod != 0) rlen ++;
            if(lprod > 0) ans = max(ans,llen);
            if(rprod > 0) ans = max(ans,rlen);
            if(lprod == 0) {
                lprod = 1;
                llen = 0;
            }
            if(rprod == 0){
                rlen = 0;
                rprod = 1;
            }
        }
        return ans;
    }
};
/**
 * @param {number[]} nums
 * @return {number}
 */
var getMaxLen = function(nums) {
    let leftToRight=0,p=1,count=0,max=0,item;
    //Process elements from left to right
    for(let i=0;i<nums.length;i++){
        if(nums[i]===0){
            p=0;
        }else if(nums[i]>0){
            p *=1
        }else if(nums[i]<0){
            p *=-1
        }
        count++;
        if(p>0){
            max = Math.max(max,count);
        }
        if(p===0){
            p=1;
            count=0;
        }
    }
    count = 0;p=1;
    //Process elements from right to left
    for(let i=nums.length-1;i>=0;i--){
        if(nums[i]===0){
            p=0;
        }else if(nums[i]>0){
            p *=1
        }else if(nums[i]<0){
            p *=-1
        }
        count++;
        if(p>0){
            max = Math.max(max,count);
        }
        if(p===0){
            p=1;
            count=0;
        }
    }
    return max;
};
Maximum Length of Subarray With Positive Product