Longest sub | C ++ implementation of the approach ; Function to return the length of the longest sub - array whose product of elements is 0 ; Driver code
#include <bits/stdc++.h> NEW_LINE 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 ; }