Given an integer array arr of size N, the task is to find the maximum length subarray whose products of element is non zero. .
Input: arr = [1, 1, 0, 2, 1, 0, 1, 6, 1]
Possible subarray whose product are non zero are [1, 1], [2, 1] and [1, 6, 1]
So maximum possible length is 3.
Input: arr = [0, 1, 2, 1, 3, 0, 0, 1]
Possible subarray whose product are non zero are [1, 2, 1, 3] and 
So maximum possible length is 4 .
- Save all the indices of zero from input array.
- Longest subarray must lie inside below three ranges:
- Start from zero index and end at first zero index – 1.
- Lies between two zero index.
- Start at last zero index + 1 and end at N-1.
- Finally find the maximum length from all cases.
Here is implementation of above approach :
Time complexity: O (N)
Auxiliary Space: 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.
- Maximum Product Subarray | Added negative product case
- Maximum length of subarray such that all elements are equal in the subarray
- Length of longest subarray with negative product
- Length of longest subarray with product greater than or equal to 0
- Length of longest subarray of length at least 2 with maximum GCD
- Maximum length of subarray such that sum of the subarray is even
- Maximum Product Subarray
- Maximum Product Subarray | Set 3
- Maximum Product Subarray | Set 2 (Using Two Traversals)
- Maximum sum subarray of even length
- Find maximum average subarray of k length
- Find subarray of Length K with Maximum Peak
- Maximum number of consecutive 1s after flipping all 0s in a K length subarray
- Maximum Length Bitonic Subarray | Set 2 (O(n) time and O(1) Space)
- Maximum length subarray with difference between adjacent elements as either 0 or 1
- Maximum Length Bitonic Subarray | Set 1 (O(n) time and O(n) space)
- Maximum length of subarray consisting of same type of element on both halves of sub-array
- Subarray with difference between maximum and minimum element greater than or equal to its length
- Find the Increasing subsequence of length three with maximum product
- Maximum product from array such that frequency sum of all repeating elements in product is less than or equal to 2 * k
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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 : SURENDRA_GANGWAR