Construct a binary string following the given constraints | Function to print a binary string which has ' a ' number of 0 ' s , ▁ ' b ' ▁ number ▁ of ▁ 1' s and there are at least ' x ' indices such that s [ i ] != s [ i + 1 ] ; Divide index value by 2 and store it into d ; If index value x is even and x / 2 is not equal to a ; Loop for d for each d print 10 ; subtract d from a and b ; Loop for b to print remaining 1 's ; Loop for a to print remaining 0 's ; Driver code
< ? php function constructBinString ( $ a , $ b , $ x ) { $ d ; $ i ; $ d = $ x / 2 ; if ( $ x % 2 == 0 && $ x / 2 != $ a ) { $ d -- ; echo 0 ; $ a -- ; } for ( $ i = 0 ; $ i < $ d ; $ i ++ ) echo "10" ; $ a = $ a - $ d ; $ b = $ b - $ d ; for ( $ i = 0 ; $ i < $ b ; $ i ++ ) { echo "1" ; } for ( $ i = 0 ; $ i < $ a ; $ i ++ ) { echo "0" ; } } $ a = 4 ; $ b = 3 ; $ x = 2 ; constructBinString ( $ a , $ b , $ x ) ; ? >