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
def constructBinString ( a , b , x ) : NEW_LINE INDENT d = x // 2 NEW_LINE if x % 2 == 0 and x // 2 != a : NEW_LINE INDENT d -= 1 NEW_LINE print ( "0" , end = " " ) NEW_LINE a -= 1 NEW_LINE DEDENT for i in range ( d ) : NEW_LINE INDENT print ( "10" , end = " " ) NEW_LINE DEDENT a = a - d NEW_LINE b = b - d NEW_LINE for i in range ( b ) : NEW_LINE INDENT print ( "1" , end = " " ) NEW_LINE DEDENT for i in range ( a ) : NEW_LINE INDENT print ( "0" , end = " " ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a , b , x = 4 , 3 , 2 NEW_LINE constructBinString ( a , b , x ) NEW_LINE DEDENT