Maximize length of Subarray of 1 's after removal of a pair of consecutive Array elements | Python program to find the maximum count of 1 s ; If arr [ i - 2 ] = = 1 then we increment the count of occurences of 1 's ; Else we initialise the count with 0 ; If arr [ i + 2 ] = = 1 then we increment the count of occurences of 1 's ; Else we initialise the count with 0 ; We get the maximum count by skipping the current and the next element . ; Driver code
def maxLengthOf1s ( arr , n ) : NEW_LINE INDENT prefix = [ 0 for i in range ( n ) ] NEW_LINE for i in range ( 2 , n ) : NEW_LINE INDENT if ( arr [ i - 2 ] == 1 ) : NEW_LINE INDENT prefix [ i ] = prefix [ i - 1 ] + 1 NEW_LINE DEDENT else : NEW_LINE INDENT prefix [ i ] = 0 NEW_LINE DEDENT DEDENT suffix = [ 0 for i in range ( n ) ] NEW_LINE for i in range ( n - 3 , - 1 , - 1 ) : NEW_LINE INDENT if ( arr [ i + 2 ] == 1 ) : NEW_LINE INDENT suffix [ i ] = suffix [ i + 1 ] + 1 NEW_LINE DEDENT else : NEW_LINE INDENT suffix [ i ] = 0 NEW_LINE DEDENT DEDENT ans = 0 NEW_LINE for i in range ( n - 1 ) : NEW_LINE INDENT ans = max ( ans , prefix [ i + 1 ] + suffix [ i ] ) NEW_LINE DEDENT print ( ans ) NEW_LINE DEDENT n = 6 NEW_LINE arr = [ 1 , 1 , 1 , 0 , 1 , 1 ] NEW_LINE maxLengthOf1s ( arr , n ) NEW_LINE