Given three integers **A**, **B** and **X**. The task is to construct a binary string **str** which has exactly **A** number of **0’s** and **B** number of **1’s** provided there has to be at least **X** indices such that **str[i] != str[i+1]**. Inputs are such that there’s always a valid solution.

**Examples:**

Input:A = 2, B = 2, X = 1

Output:1100

There are two 0’s and two 1’s and one (=X) index such that s[i] != s[i+1] (i.e. i = 1)

Input:A = 4, B = 3, X = 2

Output:0111000

**Approach:**

- Divide
**x**by**2**and store it in a variable**d**. - Check if
**d**is**even**and**d / 2 != a**, if the condition is true then print**0**and decrement**d**and**a**by**1**. - Loop from
**1 to d**and print**10**and in the end update**a = a – d**and**b = b – d**. - Finally print the remaining
**0’s**and**1’s**depending on the values of**a**and**b**.

Below is the implementation of the above approach:

## C++

`// C++ implementation of the appraoch ` `#include <iostream> ` `using` `namespace` `std; ` ` ` `// 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] ` `int` `constructBinString(` `int` `a, ` `int` `b, ` `int` `x) ` `{ ` ` ` `int` `d, i; ` ` ` ` ` `// Divide index value by 2 and store ` ` ` `// it into d ` ` ` `d = x / 2; ` ` ` ` ` `// If index value x is even and ` ` ` `// x/2 is not equal to a ` ` ` `if` `(x % 2 == 0 && x / 2 != a) { ` ` ` `d--; ` ` ` `cout << 0; ` ` ` `a--; ` ` ` `} ` ` ` ` ` `// Loop for d for each d print 10 ` ` ` `for` `(i = 0; i < d; i++) ` ` ` `cout << ` `"10"` `; ` ` ` ` ` `// subtract d from a and b ` ` ` `a = a - d; ` ` ` `b = b - d; ` ` ` ` ` `// Loop for b to print remaining 1's ` ` ` `for` `(i = 0; i < b; i++) { ` ` ` `cout << ` `"1"` `; ` ` ` `} ` ` ` ` ` `// Loop for a to print remaining 0's ` ` ` `for` `(i = 0; i < a; i++) { ` ` ` `cout << ` `"0"` `; ` ` ` `} ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `a = 4, b = 3, x = 2; ` ` ` `constructBinString(a, b, x); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## PHP

**Output:**

0111000

## Recommended Posts:

- Construct Binary Tree from String with bracket representation
- Check if a string can be formed from another string using given constraints
- Check if it is possible to convert one string into another with given constraints
- Construct binary palindrome by repeated appending and trimming
- Minimum cost to construct a string
- Python | Check if a given string is binary string or not
- Rotations of a Binary String with Odd Value
- Count of strings that can be formed using a, b and c under given constraints
- Binary tree to string with brackets
- Check divisibility of binary string by 2^k
- Count of substrings of a binary string containing K ones
- Convert String into Binary Sequence
- Ways to remove one element from a binary string so that XOR becomes zero
- Rearrange a binary string as alternate x and y occurrences
- Counting even decimal value substrings in a binary string

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.