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.

**Examples:**

Input:l = 3, m = 2, x = 4, y = 2

Output:Yes

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

Output:No

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:

## C++

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function that returns true if it is possible to ` `// make a binary string consisting of l 0's, m 1's, ` `// x "01" sub-sequences and y "10" sub-sequences ` `bool` `isPossible(` `int` `l, ` `int` `m, ` `int` `x, ` `int` `y) ` `{ ` ` ` `if` `(l * m == x + y) ` ` ` `return` `true` `; ` ` ` ` ` `return` `false` `; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `l = 3, m = 2, x = 4, y = 2; ` ` ` ` ` `if` `(isPossible(l, m, x, y)) ` ` ` `cout << ` `"Yes"` `; ` ` ` `else` ` ` `cout << ` `"No"` `; ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` `class` `sol ` `{ ` ` ` `// Function that returns true if it is possible to ` `// make a binary string consisting of l 0's, m 1's, ` `// x "01" sub-sequences and y "10" sub-sequences ` `static` `boolean` `isPossible(` `int` `l, ` `int` `m, ` `int` `x, ` `int` `y) ` `{ ` ` ` `if` `(l * m == x + y) ` ` ` `return` `true` `; ` ` ` ` ` `return` `false` `; ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String args[]) ` `{ ` ` ` `int` `l = ` `3` `, m = ` `2` `, x = ` `4` `, y = ` `2` `; ` ` ` ` ` `if` `(isPossible(l, m, x, y)) ` ` ` `System.out.print(` `"Yes"` `); ` ` ` `else` ` ` `System.out.print(` `"No"` `); ` ` ` `} ` `} ` ` ` `// This code is contributed by Arnab Kundu ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the approach ` ` ` `# Function that returns true if it is possible to ` `# make a binary string consisting of l 0's, m 1's, ` `# x "01" sub-sequences and y "10" sub-sequences ` `def` `isPossible(l, m, x, y) : ` ` ` ` ` `if` `(l ` `*` `m ` `=` `=` `x ` `+` `y) : ` ` ` `return` `True` `; ` ` ` ` ` `return` `False` `; ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `l ` `=` `3` `; m ` `=` `2` `; x ` `=` `4` `; y ` `=` `2` `; ` ` ` ` ` `if` `(isPossible(l, m, x, y)) : ` ` ` `print` `(` `"Yes"` `); ` ` ` `else` `: ` ` ` `print` `(` `"No"` `); ` ` ` `# This code is contributed by AnkitRai01 ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the approach ` `using` `System; ` ` ` `class` `sol ` `{ ` ` ` `// Function that returns true if it is possible to ` `// make a binary string consisting of l 0's, m 1's, ` `// x "01" sub-sequences and y "10" sub-sequences ` `static` `Boolean isPossible(` `int` `l, ` `int` `m, ` `int` `x, ` `int` `y) ` `{ ` ` ` `if` `(l * m == x + y) ` ` ` `return` `true` `; ` ` ` ` ` `return` `false` `; ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main(String []args) ` `{ ` ` ` `int` `l = 3, m = 2, x = 4, y = 2; ` ` ` ` ` `if` `(isPossible(l, m, x, y)) ` ` ` `Console.WriteLine(` `"Yes"` `); ` ` ` `else` ` ` `Console.WriteLine(` `"No"` `); ` `} ` `} ` ` ` `// This code is contributed by Arnab Kundu ` |

*chevron_right*

*filter_none*

**Output:**

Yes

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Number of sub-sequences of non-zero length of a binary string divisible by 3
- Given a string and an integer k, find the kth sub-string when all the sub-strings are sorted according to the given condition
- Number of sub-strings which are anagram of any sub-string of another string
- Count of sub-sequences which satisfy the given condition
- Check if two same sub-sequences exist in a string or not
- Find the permutation of first N natural numbers such that sum of i % P
_{i}is maximum possible - Longest sub string of 0's in a binary string which is repeated K times
- Distinct strings such that they contains given strings as sub-sequences
- Find the longest sub-string which is prefix, suffix and also present inside the string
- Find the longest sub-string which is prefix, suffix and also present inside the string | Set 2
- Minimum LCM and GCD possible among all possible sub-arrays
- Sum of minimum element of all sub-sequences of a sorted array
- Number of sequences which has HEAD at alternate positions to the right of the first HEAD
- 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
- Count the nodes of the tree which make a pangram when concatenated with the sub-tree nodes
- Sum of GCD of all possible sequences
- Ways to divide a binary array into sub-arrays such that each sub-array contains exactly one 1
- Find the maximum possible Binary Number from given string
- Count distinct regular bracket sequences which are not N periodic

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.