Java, DP O(n) time. Track previous positive & negative counts
At each step we keep track of the longest length of positive and negative subarrays.\n\nIf the current value is positive, then we increase the longest positive,
At each step we keep track of the longest length of positive and negative subarrays.\n\nIf the current value is positive, then we increase the longest positive, and if negative is not zero we increase it as well (since - * + = -)\n\nIf the current value is negative, then the longest positive is the longest negative + 1 (since - * - = +). The longest negative is now the longest positive + 1 (since + * - = -) or 1 if the longest positive is zero ( 0 * - = 0)\n\nIf the current value is zero, then we update longest positive and longest negative to zero, since it makes any cumulative product zero.\n\nKeep going until we process the entire array, recording the maximum longest positive value seen at each step.\n\nTricky part for me was to handle zeroes, took me some time to understand how it changed the cumulative counts.\n\n```\nclass Solution {\n    public int getMaxLen(int[] A) {\n        int max = 0, pos = 0, neg = 0;\n        \n        for (int i = 0; i < A.length; i++) {\n            if (A[i] > 0) {\n                pos = pos + 1;\n                neg = (neg > 0) ? neg + 1 : 0;\n            } else if (A[i] < 0) {\n                int tmp = pos;\n                pos = (neg > 0) ? neg + 1 : 0;\n                neg = (tmp > 0) ? tmp + 1 : 1;\n            } else {\n                pos = 0;\n                neg = 0;\n            }\n            max = Math.max(max, pos);\n        }\n        return max;\n    }\n}\n```
['Java']