# Check whether it is possible to convert A into B

Given two integers A and B. The task is to check whether it is possible to convert A into B by performing below operations any number of times.

1. Convert current number x to 2 * x.
2. Convert current number x to (10 * x) + 1.

Examples:

Input: A = 2, B = 82
Output: Yes
2 -> 4 -> 41 -> 82

Input: A = 2, B = 5
Output: No

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach: Let’s solve this problem in a reverse way – try to get the number A from B.

Note, that if B ends with 1 the last operation was to append the digit 1 to the right of the current number. Because of that let’s delete the last digit of B and move to the new number.

If the last digit is even then the last operation was to multiply the current number by 2. Because of that let’s divide B by 2 and move to the new number.

In the other cases (if B ends with odd digit except 1) the answer is No.

We need to repeat the described algorithm after every time we get a new number. If at some point, we get a number which is equal to A then the answer is Yes, and if the new number is less than A then the answer is No.

Below is the implementation of the above approach:

## C++

 `// C++ implementation of the approach ` `#include ` `using` `namespace` `std; ` ` `  `// Function that returns true if A can be ` `// converted to B with the given operations ` `bool` `canConvert(``int` `a, ``int` `b) ` `{ ` `    ``while` `(b > a) { ` ` `  `        ``// If the current number ends with 1 ` `        ``if` `(b % 10 == 1) { ` `            ``b /= 10; ` `            ``continue``; ` `        ``} ` ` `  `        ``// If the current number is divisible by 2 ` `        ``if` `(b % 2 == 0) { ` `            ``b /= 2; ` `            ``continue``; ` `        ``} ` ` `  `        ``// If above two conditions fail ` `        ``return` `false``; ` `    ``} ` ` `  `    ``// If it is possible to convert A to B ` `    ``if` `(b == a) ` `        ``return` `true``; ` `    ``return` `false``; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `A = 2, B = 82; ` ` `  `    ``if` `(canConvert(A, B)) ` `        ``cout << ``"Yes"``; ` `    ``else` `        ``cout << ``"No"``; ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java implementation of the approach ` `class` `GFG ` `{ ` ` `  `    ``// Function that returns true if A can be ` `    ``// converted to B with the given operations ` `    ``static` `boolean` `canConvert(``int` `a, ``int` `b) ` `    ``{ ` `        ``while` `(b > a)  ` `        ``{ ` ` `  `            ``// If the current number ends with 1 ` `            ``if` `(b % ``10` `== ``1``)  ` `            ``{ ` `                ``b /= ``10``; ` `                ``continue``; ` `            ``} ` ` `  `            ``// If the current number is divisible by 2 ` `            ``if` `(b % ``2` `== ``0``) ` `            ``{ ` `                ``b /= ``2``; ` `                ``continue``; ` `            ``} ` ` `  `            ``// If above two conditions fail ` `            ``return` `false``; ` `        ``} ` ` `  `        ``// If it is possible to convert A to B ` `        ``if` `(b == a) ` `            ``return` `true``; ` `        ``return` `false``; ` `    ``} ` ` `  `    ``// Driver code ` `    ``public` `static` `void` `main(String[] args)  ` `    ``{ ` ` `  `        ``int` `A = ``2``, B = ``82``; ` ` `  `        ``if` `(canConvert(A, B)) ` `            ``System.out.println(``"Yes"``); ` `        ``else` `            ``System.out.println(``"No"``); ` ` `  `    ``} ` `} ` ` `  `// This code contributed by Rajput-Ji `

## Python3

 `# Python3 implementation of the approach  ` ` `  `# Function that returns true if A can be  ` `# converted to B with the given operations  ` `def` `canConvert(a, b) :  ` ` `  `    ``while` `(b > a) : ` ` `  `        ``# If the current number ends with 1  ` `        ``if` `(b ``%` `10` `=``=` `1``) : ` `            ``b ``/``/``=` `10``;  ` `            ``continue``;  ` `         `  `        ``# If the current number is divisible by 2  ` `        ``if` `(b ``%` `2` `=``=` `0``) : ` `            ``b ``/``=` `2``;  ` `            ``continue``;  ` ` `  `        ``# If the above two conditions fail  ` `        ``return` `false;  ` `     `  `    ``# If it is possible to convert A to B  ` `    ``if` `(b ``=``=` `a) : ` `        ``return` `True``; ` `         `  `    ``return` `False``;  ` ` `  `# Driver code  ` `if` `__name__ ``=``=` `"__main__"` `:  ` ` `  `    ``A ``=` `2``; B ``=` `82``;  ` ` `  `    ``if` `(canConvert(A, B)) : ` `        ``print``(``"Yes"``);  ` `    ``else` `: ` `        ``print``(``"No"``);  ` `     `  `# This code is contributed by AnkitRai01 `

## C#

 `// C# implementation of the approach ` `using` `System; ` `class` `GFG ` `{ ` ` `  `    ``// Function that returns true if A can be ` `    ``// converted to B with the given operations ` `    ``static` `bool` `canConvert(``int` `a, ``int` `b) ` `    ``{ ` `        ``while` `(b > a)  ` `        ``{ ` ` `  `            ``// If the current number ends with 1 ` `            ``if` `(b % 10 == 1)  ` `            ``{ ` `                ``b /= 10; ` `                ``continue``; ` `            ``} ` ` `  `            ``// If the current number is divisible by 2 ` `            ``if` `(b % 2 == 0) ` `            ``{ ` `                ``b /= 2; ` `                ``continue``; ` `            ``} ` ` `  `            ``// If above two conditions fail ` `            ``return` `false``; ` `        ``} ` ` `  `        ``// If it is possible to convert A to B ` `        ``if` `(b == a) ` `            ``return` `true``; ` `        ``return` `false``; ` `    ``} ` ` `  `    ``// Driver code ` `    ``public` `static` `void` `Main()  ` `    ``{ ` ` `  `        ``int` `A = 2, B = 82; ` ` `  `        ``if` `(canConvert(A, B)) ` `            ``Console.WriteLine(``"Yes"``); ` `        ``else` `            ``Console.WriteLine(``"No"``); ` ` `  `    ``} ` `} ` ` `  `// This code is contributed by anuj_67.. `

Output:

```Yes
```

GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.