Maximum Length of Subarray With Positive Product LeetCode solution in Python
1class Solution:2def getMaxLen(self, arr: List[int]) -> int:3n = len(arr)45def solve(nums):6i, j, last_neg, neg, ans = 0, 0, None, 0, 07while j < n:8while j < n and nums[j] != 0:9if nums[j] < 0:10neg += 111last_neg = j12j += 113if neg % 2 == 0:14ans = max(ans, j - i)15elif last_neg != None:16ans = max(ans, last_neg - i, j - last_neg - 1)17i, j, neg, last_neg = j + 1, j + 1, 0, None18return ans1920return max(solve(arr), solve(arr[::-1]))