Maximum Length of Subarray With Positive Product LeetCode solution in JavaScript
1/**2* @param {number[]} nums3* @return {number}4*/5var getMaxLen = function (nums) {6let leftToRight = 0,7p = 1,8count = 0,9max = 0,10item;11//Process elements from left to right12for (let i = 0; i < nums.length; i++) {13if (nums[i] === 0) {14p = 0;15} else if (nums[i] > 0) {16p *= 1;17} else if (nums[i] < 0) {18p *= -1;19}20count++;21if (p > 0) {22max = Math.max(max, count);23}24if (p === 0) {25p = 1;26count = 0;27}28}29count = 0;30p = 1;31//Process elements from right to left32for (let i = nums.length - 1; i >= 0; i--) {33if (nums[i] === 0) {34p = 0;35} else if (nums[i] > 0) {36p *= 1;37} else if (nums[i] < 0) {38p *= -1;39}40count++;41if (p > 0) {42max = Math.max(max, count);43}44if (p === 0) {45p = 1;46count = 0;47}48}49return max;50};