1567. Maximum Length of Subarray With Positive Product LeetCode Solution
In this guide, you will get 1567. Maximum Length of Subarray With Positive Product LeetCode Solution with the best time and space complexity. The solution to Maximum Length of Subarray With Positive Product problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Maximum Length of Subarray With Positive Product solution in C++
- Maximum Length of Subarray With Positive Product solution in Java
- Maximum Length of Subarray With Positive Product solution in Python
- Additional Resources

Problem Statement of Maximum Length of Subarray With Positive Product
Given an array of integers nums, find the maximum length of a subarray where the product of all its elements is positive.
A subarray of an array is a consecutive sequence of zero or more values taken out of that array.
Return the maximum length of a subarray with positive product.
Example 1:
Input: nums = [1,-2,-3,4]
Output: 4
Explanation: The array nums already has a positive product of 24.
Example 2:
Input: nums = [0,1,-2,-3,-4]
Output: 3
Explanation: The longest subarray with positive product is [1,-2,-3] which has a product of 6.
Notice that we cannot include 0 in the subarray since that’ll make the product 0 which is not positive.
Example 3:
Input: nums = [-1,-2,-3,0,1]
Output: 2
Explanation: The longest subarray with positive product is [-1,-2] or [-2,-3].
Constraints:
1 <= nums.length <= 105
-109 <= nums[i] <= 109
Complexity Analysis
- Time Complexity: O(n);
- Space Complexity: O(1)
1567. Maximum Length of Subarray With Positive Product LeetCode Solution in C++
class Solution {
public:
int getMaxLen(vector<int>& nums) {
int ans = 0;
// the maximum length of subarrays ending in `num` with a negative product
int neg = 0;
// the maximum length of subarrays ending in `num` with a positive product
int pos = 0;
for (const int num : nums) {
pos = num == 0 ? 0 : pos + 1;
neg = num == 0 || neg == 0 ? 0 : neg + 1;
if (num < 0)
swap(pos, neg);
ans = max(ans, pos);
}
return ans;
}
};
/* code provided by PROGIEZ */
1567. Maximum Length of Subarray With Positive Product LeetCode Solution in Java
class Solution {
public int getMaxLen(int[] nums) {
int ans = 0;
// the maximum length of subarrays ending in `num` with a negative product
int neg = 0;
// the maximum length of subarrays ending in `num` with a positive product
int pos = 0;
for (final int num : nums) {
pos = num == 0 ? 0 : pos + 1;
neg = num == 0 || neg == 0 ? 0 : neg + 1;
if (num < 0) {
final int temp = pos;
pos = neg;
neg = temp;
}
ans = Math.max(ans, pos);
}
return ans;
}
}
// code provided by PROGIEZ
1567. Maximum Length of Subarray With Positive Product LeetCode Solution in Python
class Solution:
def getMaxLen(self, nums: list[int]) -> int:
ans = 0
# the maximum length of subarrays ending in `num` with a negative product
neg = 0
# the maximum length of subarrays ending in `num` with a positive product
pos = 0
for num in nums:
pos = 0 if num == 0 else pos + 1
neg = 0 if num == 0 or neg == 0 else neg + 1
if num < 0:
pos, neg = neg, pos
ans = max(ans, pos)
return ans
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.