Length of maximum product subarray

Given an integer array arr[] of size N, the task is to find the maximum length subarray whose products of element is non zero. .

Examples:

Input: arr[] = [1, 1, 0, 2, 1, 0, 1, 6, 1]
Output: 3
Explanation
Possible subarray whose product are non zero are [1, 1], [2, 1] and [1, 6, 1]
So maximum possible length is 3.

Input: arr[] = [0, 1, 2, 1, 3, 0, 0, 1]
Output: 4
Explanation
Possible subarray whose product are non zero are [1, 2, 1, 3] and [1]
So maximum possible length is 4 .

Approach:



  1. Save all the indices of zero from input array.
    • Longest subarray must lie inside below three ranges:

    • Start from zero index and end at first zero index – 1.
    • Lies between two zero index.
    • Start at last zero index + 1 and end at N-1.
  2. Finally find the maximum length from all cases.

Here is implementation of above approach :

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to find maximum 
// length subarray having non
// zero product
#include<bits/stdc++.h>
using namespace std;
  
// Function that returns the
// maximum length subarray 
// having non zero product
void Maxlength(int arr[],int N)
{
    vector<int> zeroindex;
    int maxlen;
      
    // zeroindex list to store indexex 
    // of zero
    for(int i = 0; i < N; i++)
    {
        if(arr[i] == 0)
            zeroindex.push_back(i);
    }
              
    if(zeroindex.size() == 0)
    {
          
        // If zeroindex list is empty
        // then Maxlength is as 
        // size of array
        maxlen = N;
    }
      
    // If zeroindex list is not empty 
    else
    {
          
        // for example list 1 1 0 0 1
        // is on indexex 0 1 2 3 4
          
        // first zero is on index 2
        // that means two numbers positive,
        // before index 2 so as 
        // their product is positive to
        maxlen = zeroindex[0];
          
        // Checking for other indexex 
        for(int i = 0; 
                i < zeroindex.size() - 1; i++)
        {
              
            // If the difference is greater
            // than maxlen then maxlen 
            // is updated
            if(zeroindex[i + 1]- 
               zeroindex[i] - 1 > maxlen)
            {
                maxlen = zeroindex[i + 1] - 
                         zeroindex[i] - 1;
            }
        }
          
        // To check the length of remaining
        // array after last zeroindex
        if(N - zeroindex[zeroindex.size() - 1] -
           1 > maxlen)
        {
            maxlen = N - zeroindex[
                         zeroindex.size() - 1] - 1;
        }
    }
    cout << maxlen << endl;
}
  
// Driver Code
int main()
{
    int N = 9;
    int arr[] = {7, 1, 0, 1, 2, 0, 9, 2, 1};
      
    Maxlength(arr, N);
}
      
// This code is contributed by Surendra_Gangwar

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to find 
# maximum length subarray 
# having non zero product
  
# function that returns the
# maximum length subarray 
# having non zero product
def Maxlength(arr, N):
      
    zeroindex =[]
      
    # zeroindex list to store indexex 
    # of zero
    for i in range(N):
        if(arr[i] == 0):
            zeroindex.append(i)
              
      
    if(len(zeroindex) == 0):
        # if zeroindex list is empty
        # then Maxlength is as 
        # size of array
        maxlen = N
    # if zeroindex list is not empty 
    else:
        # for example list 1 1 0 0 1
        # is on indexex 0 1 2 3 4
          
        # first zero is on index 2
        # that means two numbers positive,
        # before index 2 so as 
        # their product is positive to
          
        maxlen = zeroindex[0]
          
        # checking for other indexex 
        for i in range(0, len(zeroindex)-1):
              
            # if the difference is greater
            # than maxlen then maxlen 
            # is updated
            if(zeroindex[i + 1]\
               - zeroindex[i] - 1\
               > maxlen):
                maxlen = zeroindex[i + 1]\
                         - zeroindex[i] - 1
              
          
        # to check the length of remaining
        # array after last zeroindex
        if(N - zeroindex[len(zeroindex) - 1]\
                                 - 1 > maxlen):
            maxlen = N\
             - zeroindex[len(zeroindex) - 1] - 1
      
    print(maxlen)
  
  
# Driver Code
if __name__ == "__main__":
    N = 9
    arr = [7, 1, 0, 1, 2, 0, 9, 2, 1]
    Maxlength(arr, N)

chevron_right


Output:

3

Time complexity: O (N)
Auxiliary Space: O (N)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : SURENDRA_GANGWAR

Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.