Minimum number of operations required to maximize the Binary String | Function to find the number of operations required ; Swap 0 ' s ▁ and ▁ 1' s ; Return the answer ; Driver code
def minOperation ( s , n ) : NEW_LINE INDENT ans = 0 ; NEW_LINE i = 0 ; j = n - 1 ; NEW_LINE while ( i < j ) : NEW_LINE INDENT if ( s [ i ] == '0' and s [ j ] == '1' ) : NEW_LINE INDENT ans += 1 ; NEW_LINE i += 1 ; NEW_LINE j -= 1 ; NEW_LINE continue ; NEW_LINE DEDENT if ( s [ i ] == '1' ) : NEW_LINE INDENT i += 1 ; NEW_LINE DEDENT if ( s [ j ] == '0' ) : NEW_LINE INDENT j -= 1 ; NEW_LINE DEDENT DEDENT return ans ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 8 ; NEW_LINE s = "10100101" ; NEW_LINE print ( minOperation ( s , n ) ) ; NEW_LINE DEDENT