C++, O(n) time , O(1) space with explanation
It\'s a basic dynamic programming problem. We should track a maximum length of subarray with a positive product and a maximum length of subarray with a negative
It\'s a basic dynamic programming problem. We should track a maximum length of subarray with a positive product and a maximum length of subarray with a negative product and at each step, we update our new max length. \nThere are 3 possible variants at each step:\n* nums[i] == 0 => means that our current subarray ends at this point, because if we multiply anything by 0 then the product will be equal to 0\n* nums[i] > 0 => means that we should increase the length of subarray with the positive product and if we had the negative product we increase that too\n* nums[i] < 0 => means that we can create the positive product from negative and vice versa, so at this step our max length with positive product equal to max length with the negative product that we had +1(if we had one), and our max length of subarray with negative product equal to the max length of subarray with positive product +1;\n\nBasically, code should look like that\n```\nint getMaxLen(vector<int>& nums) {\n        int neg = 0;\n        int pos = 0;\n        int ans = 0;\n        for(int x : nums){\n            if (x==0){\n                pos = 0;\n                neg = 0;\n            }\n             if (x<0){\n\t\t\t\tint temp = pos;\n                pos = neg ? neg + 1 : 0;\n                neg = temp + 1;\n            } \n            if (x>0){\n\t\t\t\tif (neg) neg++;\n                pos++;\n            }    \n            ans = max(ans, pos);\n        }\n        return ans;\n    }\n```\nGood luck
['Dynamic Programming', 'C', 'C++']