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.
Input: A = 2, B = 2, X = 1
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
- 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:
- Check if a string can be formed from another string using given constraints
- Encrypt the given string with the following operations
- Check if given string satisfies the following conditions
- Check if it is possible to convert one string into another with given constraints
- Remove minimum characters from string to split it into three substrings under given constraints
- Find value after N operations to remove N characters of string S with given constraints
- Check if it is possible to form string B from A under the given constraints
- Generate all permutations of a string that follow given constraints
- Find smallest number with given number of digits and sum of digits under given constraints
- Generate elements of the array following given conditions
- Permute the elements of an array following given order
- Count of strings that can be formed using a, b and c under given constraints
- Divide array into two parts with equal sum according to the given constraints
- Number of ways to arrange N numbers which are in a range from 1 to K under given constraints.
- Count of elements which form a loop in an Array according to given constraints
- Check if the Matrix follows the given constraints or not
- Construct a string that has exactly K subsequences from given string
- Construct Binary Tree from String with bracket representation
- Find the final co-ordinates reached by following a sequence of directions
- Construct Binary Tree from given Parent Array representation | Iterative Approach
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.