Construct a binary string following the given constraints Last Updated : 12 Sep, 2022 Comments Improve Suggest changes Like Article Like Report 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 approach #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; } Java // Java implementation of the approach class GFG { // 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] static void 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--; System.out.print("0"); a--; } // Loop for d for each d print 10 for (i = 0; i < d; i++) System.out.print("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++) { System.out.print("1"); } // Loop for a to print remaining 0's for (i = 0; i < a; i++) { System.out.print("0"); } } // Driver code public static void main(String[] args) { int a = 4, b = 3, x = 2; constructBinString(a, b, x); } } // This code is contributed // by Mukul Singh Python3 # Python3 implementation of the above 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] def constructBinString(a, b, x): # 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 and x // 2 != a: d -= 1 print("0", end = "") a -= 1 # Loop for d for each d print 10 for i in range(d): print("10", end = "") # subtract d from a and b a = a - d b = b - d # Loop for b to print remaining 1's for i in range(b): print("1", end = "") # Loop for a to print remaining 0's for i in range(a): print("0", end = "") # Driver Code if __name__ == "__main__": a, b, x = 4, 3, 2 constructBinString(a, b, x) # This code is contributed by Rituraj_Jain C# // C# implementation of the approach using System; class GFG { // 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] static void 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--; Console.Write("0"); a--; } // Loop for d for each d print 10 for (i = 0; i < d; i++) Console.Write("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++) { Console.Write("1"); } // Loop for a to print remaining 0's for (i = 0; i < a; i++) { Console.Write("0"); } } // Driver code public static void Main() { int a = 4, b = 3, x = 2; constructBinString(a, b, x); } } // This code is contributed // by Akanksha Rai PHP <?php // PHP implementation of the // above 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] function constructBinString($a, $b, $x) { $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--; echo 0; $a--; } // Loop for d for each d print 10 for ($i = 0; $i < $d; $i++) echo "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++) { echo "1"; } // Loop for a to print remaining 0's for ($i = 0; $i < $a; $i++) { echo "0"; } } // Driver code $a = 4; $b = 3; $x = 2; constructBinString($a, $b, $x); // This code is contributed by ajit ?> JavaScript <script> // Javascript 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] function constructBinString(a, b, x) { let d, i; // Divide index value by 2 and store // it into d d = parseInt(x / 2, 10); // If index value x is even and // x/2 is not equal to a if (x % 2 == 0 && parseInt(x / 2, 10) != a) { d--; document.write("0"); a--; } // Loop for d for each d print 10 for (i = 0; i < d; i++) document.write("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++) { document.write("1"); } // Loop for a to print remaining 0's for (i = 0; i < a; i++) { document.write("0"); } } let a = 4, b = 3, x = 2; constructBinString(a, b, x); </script> Output0111000 Complexity Analysis: Time Complexity: O(max(a,b,x))Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article Construct a binary string following the given constraints S Samdare B Follow Improve Article Tags : Strings Algorithms Competitive Programming DSA binary-string Constructive Algorithms +2 More Practice Tags : AlgorithmsStrings Similar Reads Check if given Binary string follows then given condition or not Given binary string str, the task is to check whether the given string follows the below condition or not: String starts with a '1'.Each '1' is followed by empty string(""), '1', or "00".Each "00" is followed by empty string(""), '1'. If the given string follows the above criteria then print "Valid 10 min read Generate all the binary strings of N bits Given a positive integer number N. The task is to generate all the binary strings of N bits. These binary strings should be in ascending order.Examples: Input: 2Output:0 00 11 01 1Input: 3Output:0 0 00 0 10 1 00 1 11 0 01 0 11 1 01 1 1Approach: The idea is to try every permutation. For every positio 8 min read Count of Binary Strings possible as per given conditions Given two integers N and M, where N denotes the count of '0' and M denotes the count of '1', and an integer K, the task is to find the maximum number of binary strings that can be generated of the following two types: A string can consist of K '0's and a single '1'.A string can consist of K '1's and 4 min read Count ways to generate Binary String not containing "0100" Substring Given the number N, count the number of ways to create a binary string (the string that contains characters as zero or one) of size N such that it does not contain "0100" as a substring. A substring is a contiguous sequence of characters within a string. Examples: Input: N = 4Output: 15Explanation: 15+ min read Check if it is possible to form string B from A under the given constraints Given two strings A and B and two integers b and m. The task is to find that if it is possible to form string B from A such that A is divided into groups of b characters except the last group which will have characters ? b and you are allowed to pick atmost m characters from each group, and also ord 9 min read Count number of binary strings without consecutive 1âs : Set 2 Given a positive integer N, the task is to count all possible distinct binary strings of length N such that there are no consecutive 1âs. Examples: Input: N = 5 Output: 5 Explanation: The non-negative integers <= 5 with their corresponding binary representations are: 0 : 0 1 : 1 2 : 10 3 : 11 4 : 15+ min read Generate all binary strings without consecutive 1's Given an integer n, the task is to generate all binary strings of size n without consecutive 1's.Examples: Input : n = 4Output : 0000 0001 0010 0100 0101 1000 1001 1010Input : n = 3Output : 000 001 010 100 101Approach:The idea is to generate all binary strings of length n without consecutive 1's usi 6 min read Print string after removing all (â10â or â01â) from the binary string Given a binary string str consisting of only 0's and 1's, the task is to print the string after removing the occurrences of "10" and "01" from the string one by one. Print -1 if the string becomes null. Examples: Input: str = "101100" Output: -1 Explanation: In the first step, "10" at index 0 and 1 9 min read Count of substrings that start and end with 1 in given Binary String Given a binary string, count the number of substrings that start and end with 1. Examples: Input: "00100101"Output: 3Explanation: three substrings are "1001", "100101" and "101" Input: "1001"Output: 1Explanation: one substring "1001" Recommended PracticeCount SubstringsTry It!Count of substrings tha 12 min read Generate a string consisting of characters 'a' and 'b' that satisfy the given conditions Given two integers A and B, the task is to generate and print a string str such that: str must only contain the characters 'a' and 'b'.str has length A + B and the occurrence of the character 'a' is equal to A and the occurrence of character 'b' is equal to BThe sub-strings "aaa" or "bbb" must not o 6 min read Like