Minimum changes required such that the string satisfies the given condition | Function to return the minimum changes required ; To store the count of minimum changes , number of ones and the number of zeroes ; First character has to be '1' ; If condition fails changes need to be made ; Return the required count ; Driver code
< ? php function minChanges ( $ str , $ n ) { $ count = $ zeros = $ ones = 0 ; if ( $ str [ 0 ] != '1' ) { $ count ++ ; $ ones ++ ; } for ( $ i = 1 ; $ i < $ n ; $ i ++ ) { if ( $ str [ $ i ] == '0' ) $ zeros ++ ; else $ ones ++ ; if ( $ zeros > $ ones ) { $ zeros -- ; $ ones ++ ; $ count ++ ; } } return $ count ; } $ str = "0000" ; $ n = strlen ( $ str ) ; echo minChanges ( $ str , $ n ) ; ? >