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 DSA Tutorial - Learn Data Structures and Algorithms DSA (Data Structures and Algorithms) is the study of organizing data efficiently using data structures like arrays, stacks, and trees, paired with step-by-step procedures (or algorithms) to solve problems effectively. Data structures manage how data is stored and accessed, while algorithms focus on 7 min read Quick Sort QuickSort is a sorting algorithm based on the Divide and Conquer that picks an element as a pivot and partitions the given array around the picked pivot by placing the pivot in its correct position in the sorted array. It works on the principle of divide and conquer, breaking down the problem into s 12 min read Merge Sort - Data Structure and Algorithms Tutorials Merge sort is a popular sorting algorithm known for its efficiency and stability. It follows the divide-and-conquer approach. It works by recursively dividing the input array into two halves, recursively sorting the two halves and finally merging them back together to obtain the sorted array. Merge 14 min read Bubble Sort Algorithm Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in the wrong order. This algorithm is not suitable for large data sets as its average and worst-case time complexity are quite high.We sort the array using multiple passes. After the fir 8 min read Data Structures Tutorial Data structures are the fundamental building blocks of computer programming. They define how data is organized, stored, and manipulated within a program. Understanding data structures is very important for developing efficient and effective algorithms. What is Data Structure?A data structure is a st 2 min read Breadth First Search or BFS for a Graph Given a undirected graph represented by an adjacency list adj, where each adj[i] represents the list of vertices connected to vertex i. Perform a Breadth First Search (BFS) traversal starting from vertex 0, visiting vertices from left to right according to the adjacency list, and return a list conta 15+ min read Binary Search Algorithm - Iterative and Recursive Implementation Binary Search Algorithm is a searching algorithm used in a sorted array by repeatedly dividing the search interval in half. The idea of binary search is to use the information that the array is sorted and reduce the time complexity to O(log N). Binary Search AlgorithmConditions to apply Binary Searc 15 min read Insertion Sort Algorithm Insertion sort is a simple sorting algorithm that works by iteratively inserting each element of an unsorted list into its correct position in a sorted portion of the list. It is like sorting playing cards in your hands. You split the cards into two groups: the sorted cards and the unsorted cards. T 9 min read Dijkstra's Algorithm to find Shortest Paths from a Source to all Given a weighted undirected graph represented as an edge list and a source vertex src, find the shortest path distances from the source vertex to all other vertices in the graph. The graph contains V vertices, numbered from 0 to V - 1.Note: The given graph does not contain any negative edge. Example 12 min read Selection Sort Selection Sort is a comparison-based sorting algorithm. It sorts an array by repeatedly selecting the smallest (or largest) element from the unsorted portion and swapping it with the first unsorted element. This process continues until the entire array is sorted.First we find the smallest element an 8 min read Like