JavaScript | Simple, Clean O(n) Solution w/ Explanation | 100%
\n\nCode w/o Explanation:  (See Below for commented version)\n\n\nvar getMaxLen = function(nums) {\n  let maxLen = 0, startNeg = -1, startPos = -1, running = 1;
![image](https://assets.leetcode.com/users/images/6d734355-20a4-41c3-b07b-5f63eac12557_1647298349.179197.png)\n\nCode w/o Explanation:  (See Below for commented version)\n\n```\nvar getMaxLen = function(nums) {\n  let maxLen = 0, startNeg = -1, startPos = -1, running = 1;\n  \n  for (let i = 0; i < nums.length; i++) {\n    const num = nums[i];\n    \n    if (!num) {\n      startNeg = -1, startPos = -1, running = 1;\n      continue;\n    }\n    \n    running *= num\n    \n    if (num < 0 && startNeg === -1) startNeg = i;\n    if (num > 0 && startPos === -1) startPos = i;\n    \n    if (running > 0) {\n      maxLen = Math.max(maxLen, \n                startPos === -1 ? -Infinity : i - startPos + 1, \n                startNeg === -1 ? -Infinity : i - startNeg + 1)\n    } else if (startNeg !== -1) maxLen = Math.max(maxLen, i - startNeg)\n  }\n  return maxLen\n};\n```\n\nW/ Explanation:\n```\nvar getMaxLen = function(nums) {\n  //We need to track the index of the first negative value we encounter, the first positive value\n  //and our running product.\n  let maxLen = 0, startNeg = -1, startPos = -1, running = 1;\n  \n  for (let i = 0; i < nums.length; i++) {\n    const num = nums[i];\n    \n\t//If number encountered is a 0, we cannot have it in our subarray and still be positive. \n\t//Reset back to base conditions.\n    if (!num) {\n      startNeg = -1, startPos = -1, running = 1;\n      continue;\n    }\n    \n\t//Multiply running product by num.\n    running *= num\n    \n\t//If startNeg or startPos === -1, we have not yet encountered a negative or a positive in this run.\n\t//Capture first indexes for these.\n    if (num < 0 && startNeg === -1) startNeg = i;\n    if (num > 0 && startPos === -1) startPos = i;\n    \n\t//If our running total is Ever positive, that means our maxLen is previous max, or difference between i\n\t//and startPos or startNeg (whichever was first).\n    if (running > 0) {\n      maxLen = Math.max(maxLen, \n                startPos === -1 ? -Infinity : i - startPos + 1, \n                startNeg === -1 ? -Infinity : i - startNeg + 1)\n\t\t\t\t\n\t// If our running product is negative, we can take the length from after the first negative to where we currently are.  \n\t// By not doing the + 1 here, we are removing the negative number that was captured, thus making our running\n\t// product guaranteed positive between these indexes.\n    } else if (startNeg !== -1) maxLen = Math.max(maxLen, i - startNeg)\n  }\n  return maxLen\n};\n```
['JavaScript']