Java | One Pass | O(N) | Dynamic Programming
I\'d recommend solving Maximum Product Subarray before solving this problem. We can use a similar approach for solving both.\n\nWe can keep two variables to sto
I\'d recommend solving [Maximum Product Subarray](https://leetcode.com/problems/maximum-product-subarray/) before solving this problem. We can use a similar approach for solving both.\n\nWe can keep two variables to store the current length of positive and negative subarrays at any index, and update them accordingly.\n\nLet\'s say we keep `positiveLen` and `negativeLen` to denote the length of positive and negative subarrays seen so far.\nThe initialization of `positiveLen` and `negativeLen` is trival, so I\'ll skip it. Let\'s move on to how we\'ll update these.\n\nThere are 3 possible cases, \n**1. `nums[i]` is negative**\n\tSince nums[i] is negative, multiplying with a negative combo chain will give us a positive combo chain. So if there exists a negative combo chain (i.e. `negativeLen > 0`) we\'ll add one to it and update `positiveLen`. If there isn\'t any negative combo chain, there\'s no way to obtain a positive combo chain and hence the `positiveLen` should be zero.\n\t\n**2. `nums[i]` is positive**\n\tSimilar to first case, we can increase our `negativeLen` by one if there exist a negative chain or else there is no way to obtain a negative chain and we can update `negativeLen` to zero.\n\t\n**3. `nums[i]` is zero**\n\tWe reset both `positiveLen` and `negativeLen` to zero.\n\n\n```Java\nclass Solution {\n    public int getMaxLen(int[] nums) {\n        int n = nums.length;\n        \n        int positiveLen = nums[0] > 0 ? 1 : 0;\n        int negativeLen = nums[0] < 0 ? 1 : 0;\n        int maxPositiveLen = Integer.MIN_VALUE;\n        \n        maxPositiveLen = Math.max(maxPositiveLen, positiveLen);\n        \n        for (int i = 1; i < n; i++) {\n            // if nums[i] is negative\n            if (nums[i] < 0) {\n                int temp = negativeLen > 0 ? negativeLen + 1 : 0;\n                negativeLen = positiveLen + 1;\n                positiveLen = temp;\n            } else if (nums[i] > 0) {\n                negativeLen = negativeLen > 0 ? negativeLen + 1 : 0;\n                positiveLen = positiveLen + 1;\n            } else {\n                positiveLen = 0;\n                negativeLen = 0;\n            }\n            maxPositiveLen = Math.max(maxPositiveLen, positiveLen);\n        }\n        \n        return maxPositiveLen;\n    }\n}\n```
['Dynamic Programming']