Longest sub-array whose product is 0

Given an array arr[] of integer elements, the task is to find the length of the longest sub-array whose product is 0.

Examples:

Input: arr[] = {1, 2, 3, 0, 1, 2, 0}
Output: 7
{1, 2, 3, 0, 1, 2, 0} is the longest sub-array whose product is 0.

Input: arr[] = {1, 2, 3, 4, 5}
Output: 0
There is no sub-array possible whose product is 0.

Approach:

  • If there is no element in the array which is equal to 0 then there will be no sub-array possible whose product is 0.
  • If there is at least one element in the array which is equal to 0 then the longest sub-array whose product is 0 will be the complete array.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
  
// Function to return the length of the
// longest sub-array whose product
// of elements is 0
int longestSubArray(int arr[], int n)
{
    bool isZeroPresent = false;
    for (int i = 0; i < n; i++) {
        if (arr[i] == 0) {
            isZeroPresent = true;
            break;
        }
    }
  
    if (isZeroPresent)
        return n;
  
    return 0;
}
  
// Driver code
int main()
{
    int arr[] = { 1, 2, 3, 0, 1, 2, 0 };
    int n = sizeof(arr) / sizeof(arr[0]);
    cout << longestSubArray(arr, n);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of the approach
class GFG {
  
    // Function to return the length of the
    // longest sub-array whose product
    // of elements is 0
    static int longestSubArray(int arr[], int n)
    {
        boolean isZeroPresent = false;
        for (int i = 0; i < n; i++) {
            if (arr[i] == 0) {
                isZeroPresent = true;
                break;
            }
        }
  
        if (isZeroPresent)
            return n;
  
        return 0;
    }
  
    // Driver code
    public static void main(String args[])
    {
        int arr[] = { 1, 2, 3, 0, 1, 2, 0 };
        int n = arr.length;
        System.out.print(longestSubArray(arr, n));
    }
}

chevron_right


Python3

# Python3 implementation of the approach

# Function to return the length of
# the longest sub-array whose product
# of elements is 0
def longestSubArray(arr, n) :

isZeroPresent = False
for i in range (0 , n) :
if (arr[i] == 0) :
isZeroPresent = True
break

if (isZeroPresent) :
return n

return 0

# Driver code
arr = [ 1, 2, 3, 0, 1, 2, 0 ]
n = len(arr)
print(longestSubArray(arr, n))

# This code is contributed by ihritik

C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of the approach
using System;
class GFG {
  
    // Function to return the length of the
    // longest sub-array whose product
    // of elements is 0
    static int longestSubArray(int[] arr, int n)
    {
        bool isZeroPresent = false;
        for (int i = 0; i < n; i++) {
            if (arr[i] == 0) {
                isZeroPresent = true;
                break;
            }
        }
  
        if (isZeroPresent)
            return n;
  
        return 0;
    }
  
    // Driver code
    public static void Main()
    {
        int[] arr = { 1, 2, 3, 0, 1, 2, 0 };
        int n = arr.Length;
        Console.Write(longestSubArray(arr, n));
    }
}

chevron_right


PHP

Output:

7

Time Complexity: O(n)



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 : ihritik