Given four integers l, m, x and y. The task is to check whether it is possible to make a binary string consisting of l 0’s, m 1’s, x “01” and y “10” as sub-sequences in it.
Input: l = 3, m = 2, x = 4, y = 2
Possible string is “00110”. It contains 3 0’s, 2 1’s,
4 “01” sub-sequences and 2 “10” sub-sequences.
Input: l = 3, m = 2, x = 4, y = 3
No such binary string exists.
Approach: The possible string is always of the form 00…11…00…. First consists of some number of zeroes, then all ones and then the remaining number of zeros.
Let l1 be the number of zeros before ones and l2 be the number of zeros after ones then the equations are:
- l1 + l2 = l (Total number of zeros).
- l1 * m = x (Number of “01” sub-sequences).
- m * l2 = y (Number of “10” sub-sequences).
From the above three equations, we get x + y = l * m. If this equation fails for the given values then the string is not possible else print Yes.
Below is the implementation of the above approach:
- Number of sub-sequences of non-zero length of a binary string divisible by 3
- Number of flips to make binary string alternate | Set 1
- Minimum number of replacements to make the binary string alternating | Set 2
- Minimum number of swaps to make two binary string equal
- Minimum number of characters to be removed to make a binary string alternate
- Number of ways to make binary string of length N such that 0s always occur together in groups of size K
- Find all even length binary sequences with same sum of first and second half bits
- Find all even length binary sequences with same sum of first and second half bits | Iterative
- Find the maximum possible Binary Number from given string
- Deletions of "01" or "10" in binary string to make it free from "01" or "10"
- Count of operations to make a binary string"ab" free
- Minimum swaps required to make a binary string alternating
- Minimum swaps required to make a binary string divisible by 2^k
- Minimum number of moves to make a binary array K periodic
- Number of ways to erase exactly one element in the Binary Array to make XOR zero
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.