Skip to content
Related Articles

Related Articles

Improve Article

Longest sub-array whose product is 0

  • Difficulty Level : Easy
  • Last Updated : 26 Mar, 2021

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:
{1, 2, 3, 0, 1, 2, 0} is the longest sub-array whose product is 0.
Input: arr[] = {1, 2, 3, 4, 5} 
Output:
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++




// 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;
}

Java




// 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));
    }
}

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#




// 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));
    }
}

PHP




<?php
// PHP implementation of the approach
 
// Function to return the length of the
// longest sub-array whose product
// of elements is 0
function longestSubArray($arr, $n)
{
 
    $isZeroPresent = false;
    for ($i = 0; $i < $n; $i++)
    {
        if ($arr[$i] == 0)
        {
            $isZeroPresent = true;
            break;
        }
         
    }
    if ($isZeroPresent)
        return $n;
 
    return 0;
}
 
// Driver code
$arr = array( 1, 2, 3, 0, 1, 2, 0 );
$n = sizeof($arr);
echo longestSubArray($arr, $n);
 
// This code is contributed by ihritik
?>

Javascript




<script>
 
// Javascript implementation of the approach
 
// Function to return the length of the
// longest sub-array whose product
// of elements is 0
function longestSubArray(arr, n)
{
    var isZeroPresent = false;
    for (var i = 0; i < n; i++) {
        if (arr[i] == 0) {
            isZeroPresent = true;
            break;
        }
    }
 
    if (isZeroPresent)
        return n;
 
    return 0;
}
 
// Driver code
var arr = [ 1, 2, 3, 0, 1, 2, 0 ];
var n = arr.length;
document.write(longestSubArray(arr, n));
 
// This code is contributed by rutvik_56.
</script>
Output: 
7

 

Time Complexity: 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.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.




My Personal Notes arrow_drop_up
Recommended Articles
Page :