Easy to understand two pass solutions for beginners
Solution 1: TLE\n\nI try to use the same code of https://leetcode.com/problems/maximum-product-subarray/ with little modification to get positive product length
**Solution 1: TLE**\n\nI try to use the same code of https://leetcode.com/problems/maximum-product-subarray/ with little modification to get positive product length, but got couldn\'t pass last couple of test cases.\n\nTC: O(n)\nSC: O(1)\n\n```\nvar getMaxLen = function(nums) {\n    let max = -Infinity;\n    let l = 0;\n    let product = 1n; //Need to convert into BigInt\n    let s = 0;\n    \n\t//Forward pass\n    for(let i = 0; i < nums.length; i++) {\n        product = product * BigInt(nums[i]);\n        \n        if(product > max)\n            max = product;\n        \n        if(product > 0)\n            l = Math.max(l, i - s + 1); //difference of start and current to get length\n        \n        if(product === 0n) {\n            product = 1n;\n            s = i + 1; //reset start\n        }\n    }\n    \n    product = 1n;\n    s = nums.length - 1;\n    \n\t//Backward pass\n    for(let i = nums.length - 1; i >= 0; i--) {\n        product *= BigInt(nums[i]);\n        \n        if(product > max)\n            max = product;\n        \n        if(product > 0)\n            l = Math.max(l, s - i + 1);\n        \n        if(product === 0n) {\n            product = 1n;\n            s = i - 1;\n        }\n    }\n    \n    return l;\n};\n```\n\n**Solution 2: PASS**\n\nI then realised that above approach TLE because of multiplication overhead. But if we think, we don\'t actually need multiplication. All we need to take care of are the positives, negative and zero.\n\nTC: O(n)\nSC: O(1)\n\n```\nvar getMaxLen = function(nums) {\n    let product = 1;\n    let l = 0;\n    let s = -1;\n    \n    for(let i = 0; i < nums.length; i++) {\n        product = product * (nums[i] === 0 ? 0 : nums[i] > 0 ? 1 : -1); //based on number change it to 0, -1 or 1\n        \n        if(product > 0)\n            l = Math.max(l, i - s);\n        \n        if(product === 0) {\n            product = 1;\n            s = i;\n        }\n    }\n    \n    product = 1;\n    s = nums.length;\n    \n    for(let i = nums.length - 1; i >= 0; i--) {\n        product = product * (nums[i] === 0 ? 0 : nums[i] > 0 ? 1 : -1);\n        \n        if(product > 0)\n            l = Math.max(l, s - i);\n        \n        if(product === 0) {\n            product = 1;\n            s = i;\n        }\n    }\n    \n    return l;\n}\n```\n\nHOPE it helps!
['JavaScript']