LeetCode 1567: Maximum Length of Subarray With Positive Product Solution

Master LeetCode problem 1567 (Maximum Length of Subarray With Positive Product), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

1567. Maximum Length of Subarray With Positive Product

Problem Explanation

Explanation

To solve this problem, we can iterate through the array while keeping track of the running product and the indices of the first positive and negative numbers encountered so far. We can use a hashmap to store the indices of the running product values. If the running product is positive, we can calculate the length of the subarray by subtracting the current index from the index of the first positive number. If the running product is negative, we can calculate the length of the subarray by subtracting the current index from the index of the first negative number. We update the maximum length of the positive product subarray as we iterate through the array.

Algorithm:

  1. Initialize variables maxLen to store the maximum length of subarray with positive product, posIndex and negIndex to store the indices of the first positive and negative numbers encountered, and a hashmap productIndex to store the running product values.
  2. Iterate through the array nums and calculate the running product.
  3. If the running product is positive, calculate the length of the subarray using i - posIndex.
  4. If the running product is negative, calculate the length of the subarray using i - negIndex.
  5. Update the maxLen if necessary.
  6. Update the productIndex hashmap with the running product and its index.
  7. Return the maxLen.

Time Complexity: O(N) where N is the number of elements in the array. Space Complexity: O(N) for the hashmap to store running product values.

Solution Code

class Solution {
    public int getMaxLen(int[] nums) {
        int maxLen = 0;
        int posIndex = -1, negIndex = -1;
        Map<Integer, Integer> productIndex = new HashMap<>();
        productIndex.put(1, -1);
        
        int product = 1;
        for (int i = 0; i < nums.length; i++) {
            product *= nums[i];
            if (productIndex.containsKey(product)) {
                maxLen = Math.max(maxLen, i - productIndex.get(product));
            } else {
                productIndex.put(product, i);
            }
            if (product > 0) {
                maxLen = Math.max(maxLen, i - posIndex);
            } else if (product < 0) {
                maxLen = Math.max(maxLen, i - negIndex);
            } else {
                posIndex = i;
                negIndex = i;
            }
        }
        
        return maxLen;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 1567 (Maximum Length of Subarray With Positive Product)?

This page provides optimized solutions for LeetCode problem 1567 (Maximum Length of Subarray With Positive Product) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 1567 (Maximum Length of Subarray With Positive Product)?

The time complexity for LeetCode 1567 (Maximum Length of Subarray With Positive Product) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 1567 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 1567 in Java, C++, or Python.

Back to LeetCode Solutions