# Binary string with given frequencies of sums of consecutive pairs of characters

Given three integers P, Q, and R, the task is to generate a binary string with P, Q and R pairs of consecutive characters with sum 0, 1, and 2 respectively.

Examples:

Input: P = 1, Q = 2, R = 2
Output: 111001
Explanation:
Substrings “00”, “10”, “01”, and “11” have sums 0, 1, 1, and 2 repectively.
Thus the following set of substrings { {“11”}, {“11”}, {“10”}, {“00”}, {“01”} } satisfy the given constraints. Hence, the string formed by the substrings is 111001.

Input: P = 3, Q = 1, R = 0
Output: 10000

Approch: In order to solve this problem, we need to follow the following steps:

• If P and R are non-zero, then there is at least one pair of consecutive characters with sum 1. Thus, if Q provided is 0, in such a case, then no such string can be formed. Hence, return -1.
• If Q is zero, and only one of P and R is non-zero, then append 0 P+1 times if P is non-zero or append 1 R+1 times if R is non-zero.
• If all of them are non-zero:
• Append 0 and 1 P + 1 and Q + 1 times respectively.
• Append 0 and 1 alternatingly Q – 1 times.

Below is the implementation of the above approach:

## C++

 `// C++ program to generate a binary ` `// string with given frequencies ` `// of sums of consecutive ` `// pair of characters ` ` `  `#include ` `using` `namespace` `std; ` ` `  `// A Function that generates ` `// and returns the binary string ` `string build_binary_str(``int` `p, ` `                        ``int` `q, ``int` `r) ` `{ ` ` `  `    ``// P: Frequency of consecutive ` `    ``// characters with sum 0 ` `    ``// Q: Frequency of consecutive ` `    ``// characters with sum 1 ` `    ``// R: Frequency of consecutive ` `    ``// characters with sum 2 ` ` `  `    ``string ans = ``""``; ` ` `  `    ``// If no consecutive ` `    ``// character adds up to 1 ` `    ``if` `(q == 0) { ` ` `  `        ``// Not possible if both P ` `        ``// and Q are non - zero ` `        ``if` `(p != 0 && r != 0) { ` `            ``return` `"-1"``; ` `        ``} ` ` `  `        ``else` `{ ` ` `  `            ``// If P is not equal to 0 ` `            ``if` `(p) { ` ` `  `                ``// Append 0 P + 1 times ` `                ``ans = string(p + 1, ``'0'``); ` `            ``} ` `            ``else` `{ ` `                ``// Append 1 R + 1 times ` `                ``ans = string(r + 1, ``'1'``); ` `            ``} ` `        ``} ` `    ``} ` `    ``else` `{ ` ` `  `        ``// Append "01" to satisfy Q ` `        ``for` `(``int` `i = 1; i <= q + 1; i++) { ` `            ``if` `(i % 2 == 0) { ` `                ``ans += ``'0'``; ` `            ``} ` `            ``else` `{ ` `                ``ans += ``'1'``; ` `            ``} ` `        ``} ` ` `  `        ``// Append "0" P times ` `        ``ans.insert(1, string(p, ``'0'``)); ` ` `  `        ``// Append "1" R times ` `        ``ans.insert(0, string(r, ``'1'``)); ` `    ``} ` `    ``return` `ans; ` `} ` ` `  `// Driver Code ` `int` `main() ` `{ ` `    ``int` `p = 1, q = 2, r = 2; ` `    ``cout << build_binary_str(p, q, r); ` `    ``return` `0; ` `} `

Output:

```111001
```

Time Complexity: O(P + Q + R)

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.

Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.