Longest sub-array whose product is 0
Last Updated :
07 Dec, 2023
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++
#include <bits/stdc++.h>
using namespace std;
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;
}
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
class GFG {
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 ;
}
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
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
arr = [ 1 , 2 , 3 , 0 , 1 , 2 , 0 ]
n = len (arr)
print (longestSubArray(arr, n))
|
C#
using System;
class GFG {
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;
}
public static void Main()
{
int [] arr = { 1, 2, 3, 0, 1, 2, 0 };
int n = arr.Length;
Console.Write(longestSubArray(arr, n));
}
}
|
Javascript
<script>
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;
}
var arr = [ 1, 2, 3, 0, 1, 2, 0 ];
var n = arr.length;
document.write(longestSubArray(arr, n));
</script>
|
PHP
<?php
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;
}
$arr = array ( 1, 2, 3, 0, 1, 2, 0 );
$n = sizeof( $arr );
echo longestSubArray( $arr , $n );
?>
|
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.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
int longestSubArray( int arr[], int n)
{
unordered_map< int , int >mp;
for ( int i=0;i<n;i++)
{
mp[arr[i]]++;
}
if (mp[0]) return n;
else return 0;
}
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
import java.util.HashMap;
public class LongestSubArray {
static int longestSubArray( int [] arr, int n) {
HashMap<Integer, Integer> mp = new HashMap<>();
for ( int i = 0 ; i < n; i++) {
mp.put(arr[i], mp.getOrDefault(arr[i], 0 ) + 1 );
}
if (mp.containsKey( 0 )) {
return n;
} else {
return 0 ;
}
}
public static void main(String[] args) {
int [] arr = { 1 , 2 , 3 , 0 , 1 , 2 , 0 };
int n = arr.length;
System.out.println(longestSubArray(arr, n));
}
}
|
Python3
from collections import defaultdict
def longestSubArray(arr, n):
mp = defaultdict( int )
for i in range (n):
mp[arr[i]] + = 1
if mp[ 0 ]:
return n
else :
return 0
arr = [ 1 , 2 , 3 , 0 , 1 , 2 , 0 ]
n = len (arr)
print (longestSubArray(arr, n))
|
C#
using System;
using System.Collections.Generic;
class Program
{
static int LongestSubArray( int [] arr, int n)
{
Dictionary< int , int > mp = new Dictionary< int , int >();
for ( int i = 0; i < n; i++)
{
if (mp.ContainsKey(arr[i]))
{
mp[arr[i]]++;
}
else
{
mp[arr[i]] = 1;
}
}
if (mp.ContainsKey(0))
{
return n;
}
else
{
return 0;
}
}
static void Main()
{
int [] arr = { 1, 2, 3, 0, 1, 2, 0 };
int n = arr.Length;
Console.WriteLine(LongestSubArray(arr, n));
}
}
|
Javascript
function longestSubArray(arr, n) {
let mp = new Map();
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)) {
return n;
} else {
return 0;
}
}
let arr = [1, 2, 3, 0, 1, 2, 0];
let n = arr.length;
console.log(longestSubArray(arr, n));
|
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.
Share your thoughts in the comments
Please Login to comment...