Maximum Length of Subarray With Positive Product - Complete Solution Guide
Maximum Length of Subarray With Positive Product is LeetCode problem 1567, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
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 produ
Detailed Explanation
The problem asks us to find the maximum length of a contiguous subarray within a given integer array `nums` such that the product of all elements in the subarray is positive. A subarray is a consecutive sequence of elements within the array. The key constraint is that the product must be strictly positive, meaning zero values cannot be included in valid subarrays.
Solution Approach
The solution uses dynamic programming to maintain the lengths of the longest positive and negative product subarrays ending at each index. The algorithm iterates through the array, updating these lengths based on the current element. If the current element is positive, the positive length increases by one, and the negative length increases by one if it was already positive. If the current element is negative, the positive and negative lengths swap roles, incrementing if they were non-zero. If the element is zero, both lengths are reset to zero. The maximum positive length encountered so far is tracked and returned.
Step-by-Step Algorithm
- Step 1: Initialize `max_len`, `pos_len`, and `neg_len` to 0. `max_len` will store the maximum length of a subarray with a positive product found so far. `pos_len` will store the length of the longest subarray with a positive product ending at the current index. `neg_len` will store the length of the longest subarray with a negative product ending at the current index.
- Step 2: Iterate through the input array `nums`.
- Step 3: For each element `num` in `nums`:
- Step 4: If `num` is positive:
- Step 4a: Increment `pos_len` by 1.
- Step 4b: If `neg_len` is greater than 0, increment `neg_len` by 1 (because multiplying a negative product by a positive number maintains the negative product).
- Step 5: Else if `num` is negative:
- Step 5a: Store the value of `neg_len` in a temporary variable `temp` before modifying `pos_len` and `neg_len`.
- Step 5b: If `neg_len` is greater than 0, set `pos_len` to `neg_len` + 1. Otherwise, set `pos_len` to 0 (because multiplying a negative product by a negative number results in a positive product).
- Step 5c: Set `neg_len` to `temp + 1` if it was greater than 0, 0 otherwise. `temp+1` since we now have 1 more neg number. If temp was 0 we still need to add the negative number to the negative len and it will be 1
- Step 5d: `neg_len = pos_len + 1` as a negative number times a positive makes it negative. It will be 1 if pos_len is 0.
- Step 6: Else (if `num` is zero):
- Step 6a: Reset both `pos_len` and `neg_len` to 0 (because any subarray containing a zero will have a product of zero).
- Step 7: Update `max_len` to be the maximum of its current value and `pos_len`.
- Step 8: After iterating through all elements in `nums`, return `max_len`.
Key Insights
- Insight 1: The sign of the product is determined by the number of negative numbers. An even number of negative numbers results in a positive product, while an odd number results in a negative product.
- Insight 2: Zeros act as dividers, splitting the array into independent subarrays. The maximum length positive product subarray must lie entirely within a segment bounded by zeros (or the start/end of the array).
- Insight 3: We can keep track of the lengths of the longest positive and negative product subarrays ending at each index. When encountering a negative number, the positive and negative lengths swap roles (with adjustments for zero lengths).
Complexity Analysis
Time Complexity: O(n)
Space Complexity: O(1)
Topics
This problem involves: Array, Dynamic Programming, Greedy.
Companies
Asked at: Arcesium.