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.

- Append

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 <bits/stdc++.h> ` `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; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

111001

**Time Complexity:*** O(P + Q + R)*

## Recommended Posts:

- XOR of Prime Frequencies of Characters in a String
- Print characters and their frequencies in order of occurrence using Binary Tree
- Check whether the frequencies of all the characters in a string are prime or not
- Sum and Product of Prime Frequencies of Characters in a String
- Rearrange the characters of the string such that no two adjacent characters are consecutive English alphabets
- Character whose frequency is equal to the sum of frequencies of other characters of the given string
- Generate a Number in Decreasing order of Frequencies of characters of a given String
- Minimize Cost to sort a String in Increasing Order of Frequencies of Characters
- Generate a Binary String without any consecutive 0's and at most K consecutive 1's
- Group consecutive characters of same type in a string
- Reduce the string by removing K consecutive identical characters
- Minimum Cost of deletions such that string does not contains same consecutive characters
- Python | Split string in groups of n consecutive characters
- Count pairs of characters in a string whose ASCII value difference is K
- Number of ways to insert two pairs of parentheses into a string of N characters
- Check if a binary string contains consecutive same or not
- Check if a binary string has two consecutive occurrences of one everywhere
- Maximum Consecutive Zeroes in Concatenated Binary String
- Length of longest consecutive ones by at most one swap in a Binary String
- Python - Consecutive elements maximum frequencies

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.