
Maximum Length of Subarray With Positive Product (medium)
Problem Statement
Given an integer array nums, find the maximum length of a subarray where the product of all its elements is greater than 0.
A subarray is a sequence of consecutive elements from the original array.
Examples
-
Example 1:
- Input:
nums = [1, -2, 3, 4] - Expected Output:
2 - Justification: The longest subarray with a positive product is
[3, 4], with a length of 2.
- Input:
-
Example 2:
- Input:
nums = [-1, -2, -3, -4] - Expected Output:
4 - Justification: The entire array produces a positive product since an even number of negative numbers multiply to a positive product. Hence, the longest length is 4.
- Input:
-
Example 3:
- Input:
nums = [0, -1, 2, -3, 4, -5, 6] - Expected Output:
5 - Justification: The longest subarray with a positive product is
[2, -3, 4, -5, 6], with a length of 5.
- Input:
Try it yourself
Try solving this question here:
Python3
Python3
. . . .
.....
.....
.....
Unlock this and all other premium problems.
No code editor for this lesson
This lesson focuses on concepts and theory