Java | In-depth Explanation | O(n) time | O(1) space
Intuition\n\n  We need to have two variables positive and negative to track the length of positive product and the length of negative product to arrays. Why? Si
**Intuition**\n\n  We need to have two variables `positive` and `negative` to track the length of positive product and the length of negative product to arrays. Why? Since we gunna return the max length of positive product, of course we need to track it. But why we need to track length negative product? We need to do so because we don\'t know if the next element is negative or positive.  \n  \n  **Algorithm**\n  \n  1. If we see `num == 0`, we reset both `positive` and `negative`.\n  2. If `num > 0`, we update both `positive` and `negative`.\n      **a.** `positive += 1`\n\t  **b.** `negative == 0 ? 0 : negative + 1` If we don\'t have any negative num before, the length of negative product would still be zero now. However, if we had any negative nums, then we need to update as `negative + 1`. \n\t  For example, nums[ ] = [-3, 1, 2].  `negative = 3` when we iterate over the array\n  3. If `num < 0`, we update both `positive` and `negative` as we do in step 2. Then we **swap** the value of two variables. \n  4. update `max` in the end.\n\n```\nelements      :   9    5    8    2    -6    4    -3    0    2    -5    15    10    -7    9    -2\npositive      :   1    2    3    4     0    1     7    0    1     0     1     2     5    6     5\nnegative      :   0    0    0    0     5    6     2    0    0     2     3     4     3    4     7\n```\n\n```\nclass Solution \n{\n    public int getMaxLen(int[] nums) \n    {\n        // O(n) time | O(1) space\n        int positive = 0, negative = 0;\n        int max = 0;\n        \n        for(int n : nums)\n        {\n            if(n == 0)\n            {\n                positive = 0;\n                negative = 0;\n            }\n            else if(n > 0)\n            {\n                positive += 1;\n                negative = negative == 0 ? 0 : negative+1;\n            }\n            else\n            {\n                // swap positive and negative\n                int tmp = positive;\n                positive = negative == 0 ? 0 : negative+1;\n                negative = tmp+1;\n            }\n            max = Math.max(max, positive);\n        }\n        return max;\n    }\n}\n```
['Java']