Construct a binary string following the given constraints | C ++ implementation of the approach ; 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
#include <iostream> NEW_LINE using namespace std ; int constructBinString ( int a , int b , int x ) { int d , i ; d = x / 2 ; if ( x % 2 == 0 && x / 2 != a ) { d -- ; cout << 0 ; a -- ; } for ( i = 0 ; i < d ; i ++ ) cout << "10" ; a = a - d ; b = b - d ; for ( i = 0 ; i < b ; i ++ ) { cout << "1" ; } for ( i = 0 ; i < a ; i ++ ) { cout << "0" ; } } int main ( ) { int a = 4 , b = 3 , x = 2 ; constructBinString ( a , b , x ) ; return 0 ; }