# 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.

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

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 ` `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); ` `    ``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

 ` `

Output:

```7
```

Time Complexity: O(n)

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

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

Article Tags :
Practice Tags :

1

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