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>
usingnamespacestd;
// Function to return the length of the
// longest sub-array whose product
// of elements is 0
intlongestSubArray(intarr[], intn)
{
boolisZeroPresent = false;
for(inti = 0; i < n; i++) {
if(arr[i] == 0) {
isZeroPresent = true;
break;
}
}
if(isZeroPresent)
returnn;
return0;
}
// Driver code
intmain()
{
intarr[] = { 1, 2, 3, 0, 1, 2, 0 };
intn = sizeof(arr) / sizeof(arr[0]);
cout << longestSubArray(arr, n);
return0;
}
Java
// Java implementation of the approach
classGFG {
// Function to return the length of the
// longest sub-array whose product
// of elements is 0
staticintlongestSubArray(intarr[], intn)
{
booleanisZeroPresent = false;
for(inti = 0; i < n; i++) {
if(arr[i] == 0) {
isZeroPresent = true;
break;
}
}
if(isZeroPresent)
returnn;
return0;
}
// Driver code
publicstaticvoidmain(String args[])
{
intarr[] = { 1, 2, 3, 0, 1, 2, 0};
intn = 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
deflongestSubArray(arr, n) :
isZeroPresent =False
fori inrange(0, n) :
if(arr[i] ==0) :
isZeroPresent =True
break
if(isZeroPresent) :
returnn
return0
# 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
usingSystem;
classGFG {
// Function to return the length of the
// longest sub-array whose product
// of elements is 0
staticintlongestSubArray(int[] arr, intn)
{
boolisZeroPresent = false;
for(inti = 0; i < n; i++) {
if(arr[i] == 0) {
isZeroPresent = true;
break;
}
}
if(isZeroPresent)
returnn;
return0;
}
// Driver code
publicstaticvoidMain()
{
int[] arr = { 1, 2, 3, 0, 1, 2, 0 };
intn = arr.Length;
Console.Write(longestSubArray(arr, n));
}
}
Javascript
<script>
// Javascript implementation of the approach
// Function to return the length of the
// longest sub-array whose product
// of elements is 0
functionlongestSubArray(arr, n)
{
varisZeroPresent = false;
for(vari = 0; i < n; i++) {
if(arr[i] == 0) {
isZeroPresent = true;
break;
}
}
if(isZeroPresent)
returnn;
return0;
}
// Driver code
vararr = [ 1, 2, 3, 0, 1, 2, 0 ];
varn = arr.length;
document.write(longestSubArray(arr, n));
// This code is contributed by rutvik_56.
</script>
PHP
<?php
// PHP implementation of the approach
// Function to return the length of the
// longest sub-array whose product
// of elements is 0
functionlongestSubArray($arr, $n)
{
$isZeroPresent= false;
for($i= 0; $i< $n; $i++)
{
if($arr[$i] == 0)
{
$isZeroPresent= true;
break;
}
}
if($isZeroPresent)
return$n;
return0;
}
// Driver code
$arr= array( 1, 2, 3, 0, 1, 2, 0 );
$n= sizeof($arr);
echolongestSubArray($arr, $n);
// This code is contributed by ihritik
?>
Output
7
Time Complexity: O(n), where n represents the size of the given array. Auxiliary Space: O(1), no extra space is required, so it is a constant.
Method2:Using Hashing(unordered Map)
Approach:
1. We can use an unordered_map to store the frequency of elements in the array. 2. If the map contains a 0, then the entire array is a sub-array whose product is 0, so we return n. 3. Otherwise, there is no sub-array whose product is 0, so we return 0.
// Uncomment the line below if running in Visual Studio or an environment that doesn't automatically pause
// Console.ReadLine();
}
}
// This code is contributed by Dwaipayan Bandyopadhyay
Javascript
functionlongestSubArray(arr, n) {
let mp = newMap();
for(let i = 0; i < n; i++) {
if(mp.has(arr[i])) {
mp.set(arr[i], mp.get(arr[i]) + 1);
} else{
mp.set(arr[i], 1);
}
}
if(mp.has(0)) {
returnn;
} else{
return0;
}
}
let arr = [1, 2, 3, 0, 1, 2, 0];
let n = arr.length;
console.log(longestSubArray(arr, n));
Output
7
Time Complexity: O(n), where n represents the size of the given array. Auxiliary Space: O(n), we use extra space as an unordered map.
Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!