Length of second longest sequence of consecutive 1 s in a binary array | ''Function to find maximum and second maximum length ; '' Initialise maximum length ; '' Initialise second maximum length ; '' Initialise count ; '' Iterate over the array ; '' If sequence ends ; '' Reset count ; '' Otherwise ; '' Increase length  of current sequence ; '' Update maximum ; '' Traverse the given array ; '' If sequence continues ; '' Increase length  of current sequence ; '' Update second max ; '' Reset count when 0 is found ; '' Print the result ; ''Driver Code ; '' Given array ; '' Function Call
def FindMax ( arr , N ) : NEW_LINE INDENT maxi = - 1 NEW_LINE maxi2 = - 1 NEW_LINE count = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT if ( arr [ i ] == 0 ) : NEW_LINE INDENT count = 0 NEW_LINE DEDENT else : NEW_LINE INDENT count += 1 NEW_LINE maxi = max ( maxi , count ) NEW_LINE DEDENT DEDENT for i in range ( N ) : NEW_LINE INDENT if ( arr [ i ] == 1 ) : NEW_LINE INDENT count += 1 NEW_LINE if ( count > maxi2 and count < maxi ) : NEW_LINE INDENT maxi2 = count NEW_LINE DEDENT DEDENT if ( arr [ i ] == 0 ) : NEW_LINE INDENT count = 0 NEW_LINE DEDENT DEDENT maxi = max ( maxi , 0 ) NEW_LINE maxi2 = max ( maxi2 , 0 ) NEW_LINE print ( maxi2 ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 1 , 1 , 0 , 0 , 1 , 1 ] NEW_LINE N = len ( arr ) NEW_LINE FindMax ( arr , N ) NEW_LINE DEDENT