Maximum Length of Subarray With Positive Product LeetCode solution in Java
1class Solution {2public int getMaxLen(int[] nums) {3int first_negative = -1;4int zero_position = -1;5int count_neg = 0;6int res = 0;7for (int i = 0; i < nums.length; i++) {8if (nums[i] < 0) {9count_neg = count_neg + 1;10if (first_negative == -1) {11first_negative = i;12}13}14if (nums[i] == 0) {15count_neg = 0;16zero_position = i;17first_negative = -1;18} else {19if (count_neg % 2 == 0) {20res = Math.max(i - zero_position, res);21} else {22res = Math.max(i - first_negative, res);23}24}25}26return res;27}28}