0% completed
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:
Solution
To solve this problem, we maintain a length of the longest subarray while traversing through the array once. Our approach relies on understanding how the sign of the product changes with each element. Specifically, a positive number doesn't change the product's sign, a negative number flips it, and zero resets it. Hence, we track the length of the subarray until the current position that leads to a positive and negative product separately, updating them based on the current number's sign.
This method is effective because it simplifies the problem to tracking the sign changes due to negative numbers and resets caused by zeros, which can be done in a single pass through the array. By keeping track of the lengths of subarrays that lead to both positive and negative products, we can efficiently find the maximum length of the subarray with a positive product without needing to calculate the actual products or examine every possible subarray explicitly.
Step-by-Step Algorithm
- Initialize two counters:
positiveCount
to 0 andnegativeCount
to 0. These track the length of the longest subarray ending at the current index with a positive and negative product, respectively. - Initialize
maxLength
to 0 to keep track of the maximum length of subarrays with positive products. - Iterate through the array
nums
:-
If the current element is positive, increment
positiveCount
by 1. IfnegativeCount
is not 0, increment it by 1 as well, since a negative product can become positive if multiplied by a negative number. -
If the current element is negative, swap
positiveCount + 1
andnegativeCount
if value ofnegativeCount
is 0. Otherwise, swappositiveCount + 1
andnegativeCount + 1
. -
If the current element is zero, reset both
positiveCount
andnegativeCount
to 0 since the product of any subarray containing zero is zero. -
Update
maxLength
with the maximum value between itself andpositiveCount
.
-
- The answer is the value of
maxLength
after iterating through the entire array.
Algorithm Walkthrough
Let's consider the input [0, -1, 2, -3, 4, -5, 6]
.
-
Initialization:
positiveCount = 0
: Tracks the length of the current subarray with a positive product.negativeCount = 0
: Tracks the length of the current subarray with a negative product.maxLength = 0
: Keeps track of the maximum length of any subarray encountered with a positive product.
-
First Element (0):
- The first element is
0
, which resets bothpositiveCount
andnegativeCount
to0
. positiveCount = 0
,negativeCount = 0
.maxLength
remains0
.
- The first element is
-
Second Element (-1):
- The second element is
-1
, a negative number. - Swap
positiveCount
andnegativeCount
(both are0
at this point, so swapping has no effect). - Increment
negativeCount
by 1:negativeCount = 1
. positiveCount
remains0
.maxLength
remains0
(since there's no positive product subarray yet).
- The second element is
-
Third Element (2):
- The third element is
2
, a positive number. - Increment
positiveCount
(since it's positive):positiveCount = 1
. - Increment
negativeCount
(since a negative subarray exists):negativeCount = 2
. - Update
maxLength
to1
.
- The third element is
-
Fourth Element (-3):
- The fourth element is
-3
, a negative number. - Swap
positiveCount + 1
andnegativeCount + 1
then incrementnegativeCount
:positiveCount
becomes3
andnegativeCount
becomes2
. - Increment
negativeCount
by 1:negativeCount = 2
. - Update
maxLength
to2
(the length of subarray leading up to the second element).
- The fourth element is
-
Fifth Element (4):
- The fifth element is
4
, a positive number. - Increment
positiveCount
:positiveCount = 4
. - Increment
negativeCount
(since a negative subarray exists):negativeCount = 3
. - Update
maxLength
to4
.
- The fifth element is
-
Sixth Element (-5):
- The sixth element is
-5
, a negative number. - Swap
positiveCount + 1
andnegativeCount + 1
then incrementnegativeCount
:positiveCount
becomes4
andnegativeCount
becomes5
. - Increment
negativeCount
by 1:negativeCount = 5
. maxLength
remains4
.
- The sixth element is
-
Seventh Element (6):
- The seventh element is
6
, a positive number. - Increment
positiveCount
:positiveCount = 5
. - Increment
negativeCount
(since a negative subarray exists):negativeCount = 6
. - Update
maxLength
to5
.
- The seventh element is
-
Final Output:
- After processing all elements, the
maxLength
found is5
.
- After processing all elements, the
Code
Complexity Analysis
-
Time Complexity: O(n) where
n
is the number of elements in the array. This is because the algorithm iterates through the array exactly once, performing a constant amount of work for each element. -
Space Complexity: O(1) as the space required does not grow with the size of the input array. Only a fixed number of variables are used, regardless of the array size.
.....
.....
.....
Table of Contents
Contents are not accessible
Contents are not accessible
Contents are not accessible
Contents are not accessible
Contents are not accessible