Length of maximum product subarray | function that returns the maximum length subarray having non zero product ; zeroindex list to store indexex of zero ; if zeroindex list is empty then Maxlength is as size of array ; if zeroindex list is not empty ; first zero is on index 2 that means two numbers positive , before index 2 so as their product is positive to ; checking for other indexex ; if the difference is greater than maxlen then maxlen is updated ; to check the length of remaining array after last zeroindex ; Driver Code
def Maxlength ( arr , N ) : NEW_LINE INDENT zeroindex = [ ] NEW_LINE for i in range ( N ) : NEW_LINE INDENT if ( arr [ i ] == 0 ) : NEW_LINE INDENT zeroindex . append ( i ) NEW_LINE DEDENT DEDENT if ( len ( zeroindex ) == 0 ) : NEW_LINE INDENT maxlen = N NEW_LINE DEDENT else : NEW_LINE INDENT maxlen = zeroindex [ 0 ] NEW_LINE for i in range ( 0 , len ( zeroindex ) - 1 ) : NEW_LINE INDENT if ( zeroindex [ i + 1 ] \ - zeroindex [ i ] - 1 \ > maxlen ) : NEW_LINE INDENT maxlen = zeroindex [ i + 1 ] - zeroindex [ i ] - 1 NEW_LINE DEDENT DEDENT if ( N - zeroindex [ len ( zeroindex ) - 1 ] \ - 1 > maxlen ) : NEW_LINE INDENT maxlen = N - zeroindex [ len ( zeroindex ) - 1 ] - 1 NEW_LINE DEDENT DEDENT print ( maxlen ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 9 NEW_LINE arr = [ 7 , 1 , 0 , 1 , 2 , 0 , 9 , 2 , 1 ] NEW_LINE Maxlength ( arr , N ) NEW_LINE DEDENT