Maximum Length of Subarray With Positive Product

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.
Find the maximum length of a subarray with a positive product.

Example 1:

Input: nums = [0,1,-2,-3,-4]
Output: 3

Approach

Java

import java.util.ArrayList;
import java.util.List;

public class MaxLenSubarrayWithPositiveProduct {
    public static void main(String[] args) {
        int nums[] = { 01, -2, -3, -4 };
        System.out.println(getMaxLen(nums));
    }

    // function to find the maximum length
    // subarray with positive product
    static int getMaxLen(int[] nums) {
        int ans = 0;
        int pos = 0, neg = 0;
        int index = 0;
        int n = nums.length;
        List<Integerv = new ArrayList<Integer>();

        for (int i = 0; i < n; i++) {
            if (nums[i] == 0) {
                if (neg % 2 == 0)
                    ans = Math.max(ans, pos + neg);
                else {
                    int start = v.get(0);
                    int last = v.get(v.size() - 1);
                    ans = Math.max(ans, Math.max(i - 1 - start, last - index));

                }
                neg = 0;
                pos = 0;
                v = new ArrayList<Integer>();
                index = i + 1;
            } else if (nums[i] < 0) {
                v.add(i);
                neg++;

            } else
                pos++;
        }
        if (v.size() != 0) {
            int start = n - 1 - v.get(0);
            int last = v.get(v.size() - 1);
            ans = Math.max(ans, Math.max(start, last - index));
        }
        if (neg % 2 == 0)
            ans = Math.max(ans, neg + pos);
        return ans;
    }
}

C++

#include <bits/stdc++.h>
using namespace std;

//function to find the maximum length
//subarray with positive product
int getMaxLen(vector<int>& nums) {
        int ans=0;
        int pos=0,neg=0;
        vector<intv;
        int index=0;
        int n=nums.size();
        for(int i=0;i<n;i++)
        {
            if(nums[i]==0)
            {
                if(neg%2==0)
                      ans=max(ans,pos+neg);
                else
                {
                    int start=v[0];
                    int last=v[v.size()-1];
                    ans=max(ans,max(i-1-start,last-index));
                    
                }
                neg=0;
                pos=0;
                v.clear();
                index=i+1;
            }
            else if(nums[i]<0)
            {
                v.push_back(i);
                neg++;
                

            }
            else
                  pos++;
        }
        if(v.size()!=0)
        {
                 int start=n-1-v[0];
                 int last=v[v.size()-1];
                  ans=max(ans,max(start,last-index));
        }
       if(neg%2==0)
               ans=max(ans,neg+pos);
        return ans;
}
int main()
{
    vector<intnums ={0,1,-2,-3,-4};
    cout<<getMaxLen(nums);
    return 0;
}



No comments:

Post a Comment