[Python3/Go/Java] Dynamic Programming O(N) time O(1) space
Explanation\n"pos[i]", "neg[i]" represent longest consecutive numbers ending with nums[i] forming a positive/negative product.\n\n#### Complexity\ntime: O(N)\ns
### Explanation\n"pos[i]", "neg[i]" represent longest consecutive numbers ending with nums[i] forming a positive/negative product.\n\n#### Complexity\n`time`: O(N)\n`space`: O(N)\n\n#### Python3:\n\n```python\ndef getMaxLen(self, nums: List[int]) -> int:\n\tn = len(nums)\n\tpos, neg = [0] * n, [0] * n\n\tif nums[0] > 0: pos[0] = 1\n\tif nums[0] < 0: neg[0] = 1\n\tans = pos[0]\n\tfor i in range(1, n):\n\t\tif nums[i] > 0:\n\t\t\tpos[i] = 1 + pos[i - 1]\n\t\t\tneg[i] = 1 + neg[i - 1] if neg[i - 1] > 0 else 0\n\t\telif nums[i] < 0:\n\t\t\tpos[i] = 1 + neg[i - 1] if neg[i - 1] > 0 else 0\n\t\t\tneg[i] = 1 + pos[i - 1]\n\t\tans = max(ans, pos[i])\n\treturn ans\n```\n\n\n#### Golang:\n\n```Golang\nfunc getMaxLen(nums []int) int {\n    n := len(nums)\n    pos := make([]int, n)\n    neg := make([]int, n)\n    if nums[0] > 0 {\n        pos[0] = 1\n    } else if nums[0] < 0 {\n        neg[0] = 1\n    }\n    ans := pos[0]\n    for i := 1; i < n; i++ {\n        if nums[i] > 0 {\n            pos[i] = 1 + pos[i - 1]\n            if neg[i - 1] > 0 {\n                neg[i] = 1 + neg[i - 1]\n            } else {\n                neg[i] = 0\n            }\n        } else if nums[i] < 0 {\n            if neg[i - 1] > 0 {\n                pos[i] = 1 + neg[i - 1]\n            } else {\n                pos[i] = 0\n            }\n            neg[i] = 1 + pos[i - 1]\n        }\n        ans = Max(ans, pos[i])\n    }\n    return ans\n}\n\nfunc Max(a, b int) int {\n    if a > b {\n        return a\n    }\n    return b\n}\n```\n\n\n#### Java:\n\n```Java\npublic int getMaxLen(int[] nums) {\n\tint n = nums.length;\n\tint[] pos = new int[n];\n\tint[] neg = new int[n];\n\tif (nums[0] > 0) pos[0] = 1;\n\tif (nums[0] < 0) neg[0] = 1;\n\tint ans = pos[0];\n\tfor (int i = 1; i < n; i++) {\n\t\tif (nums[i] > 0) {\n\t\t\tpos[i] = 1 + pos[i - 1];\n\t\t\tneg[i] = neg[i - 1] > 0 ? 1 + neg[i - 1]:0;\n\t\t} else if (nums[i] < 0) {\n\t\t\tpos[i] = neg[i - 1] > 0 ? 1 + neg[i - 1]:0;\n\t\t\tneg[i] = 1 + pos[i - 1];\n\t\t}\n\t\tans = Math.max(ans, pos[i]);\n\t}\n\treturn ans;\n}\n```\n\n\n### Space Optimization\n\n### Complexity\n`time`: O(N)\n`space`: O(1)\n\nThanks @Spin0za for pointing out.\nSince only the previous one value of pos/neg is used, we can use 2 variables instead of 2 lists.\n\n#### Python3\n```python\ndef getMaxLen(self, nums: List[int]) -> int:\n\tn = len(nums)\n\tpos, neg = 0, 0\n\tif nums[0] > 0: pos = 1\n\tif nums[0] < 0: neg = 1\n\tans = pos\n\tfor i in range(1, n):\n\t\tif nums[i] > 0:\n\t\t\tpos = 1 + pos\n\t\t\tneg = 1 + neg if neg > 0 else 0\n\t\telif nums[i] < 0:\n\t\t\tpos, neg = 1 + neg if neg > 0 else 0, 1 + pos\n\t\telse:\n\t\t\tpos, neg = 0, 0\n\t\tans = max(ans, pos)\n\treturn ans\n```
['Go', 'Python3']