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] <= 109When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force way to solve this is to look at every possible section of the list. For each section, we will determine if the product is positive, and if so, keep track of its length.
Here's how the algorithm would work step-by-step:
def get_max_len_positive_product(
numbers
):
max_length = 0
# Iterate through all possible start indices
for start_index in range(len(numbers)):
# Iterate through all possible end indices
for end_index in range(start_index, len(numbers)):
current_product = 1
# Calculate the product of the current subarray
for index in range(start_index, end_index + 1):
current_product *= numbers[index]
# Check if the product is positive
if current_product > 0:
# Update max_length if necessary
subarray_length = end_index - start_index + 1
# Update maximum length found
if subarray_length > max_length:
max_length = subarray_length
return max_lengthTo find the longest section where multiplying all the numbers gives a positive result, we need to cleverly track how many positive and negative numbers we've seen so far. The core idea is to keep track of where the first negative number was encountered because an even number of negatives results in a positive product.
Here's how the algorithm would work step-by-step:
def find_max_positive_product_length(nums):
maximum_length = 0
start_index = -1
first_negative_index = -1
negative_count = 0
for i, num in enumerate(nums):
if num == 0:
start_index = -1
first_negative_index = -1
negative_count = 0
continue
if start_index == -1:
start_index = i
if num < 0:
negative_count += 1
# Store the first negative index to handle odd negative counts.
if first_negative_index == -1:
first_negative_index = i
# Even negative count means the whole subarray has a positive product.
if negative_count % 2 == 0:
maximum_length = max(maximum_length, i - start_index + 1)
# Odd negative count means we exclude the subarray before the first negative.
else:
maximum_length = max(maximum_length, i - first_negative_index)
return maximum_length| Case | How to Handle |
|---|---|
| Empty or null input array | Return 0, as there is no subarray with a positive product. |
| Array with a single element (positive) | Return 1, as the single element forms a subarray of length 1 with a positive product. |
| Array with a single element (negative) | Return 0, as the single element forms a subarray of length 1 with a negative product. |
| Array containing only zeros | Return 0, as no subarray can have a positive product. |
| Array with alternating positive and negative numbers | The algorithm must correctly track the length of positive and negative subarrays, resetting when a zero is encountered. |
| Array with a large number of zeros | The algorithm should efficiently reset and restart subarray calculations at each zero encountered, preventing unnecessary computations. |
| Array with extreme negative numbers (potential for integer overflow with multiplication) | The problem statement likely constrains the range, or the test should handle potential overflow, but consider using a `long` data type if overflow is a concern with the given data type limits. |
| Array where all numbers are negative | The solution needs to track the first and last negative number indices to calculate the length of the longest positive product subarray formed by excluding either the first or last negative number. |