O(n) time and O(1) space: Simple and Well commented easy to understand code
\nclass Solution {\n    public int getMaxLen(int[] nums) {\n        \n        int res=0; //result\n        int p=0,n=0,fn=-1;\n        //p=count of positive num
```\nclass Solution {\n    public int getMaxLen(int[] nums) {\n        \n        int res=0; //result\n        int p=0,n=0,fn=-1;\n        //p=count of positive numbers till current index from start or after any zero so far\n        //n=count of negative numbers till current index from start or after any zero so far\n        //fn=index of first negative number till current index from start or after any zero so far\n        for(int i=0;i<nums.length;i++){\n            if(nums[i]==0){\n                //a zero can not be a part of any subarray and thus we break the count from here and reset the counting variables\n                p=0;\n                n=0;\n                fn=-1;\n                continue;\n            }\n            if(nums[i]>0){\n                p++;\n            }\n            else{\n                n++;\n                if(n==1)\n                    fn=i;     //keep track of the index of first occurence of a negative number from start or after any zero so far \n            }\n            \n            if(n%2==0){\n                //if count of negative numbers is even, then length of current subarray is sum of positive and negative number counts\n                res=Math.max(res,n+p);\n            }\n            else{\n                //if count of negative numbers is odd, we must exclude the prefix of current array till first occurence of a negative number(denoted by fn)\n                res=Math.max(res,i-fn);\n            }\n        }\n        return res;\n    }\n}\n```
[]