Convert X into Y by repeatedly multiplying X with 2 or appending 1 at the end
Last Updated :
21 Sep, 2023
Given two positive integers X and Y, the task is to check if it is possible to convert the number X into Y, either by multiplying X by 2 or appending 1 at the end of X. If it is possible to convert X into Y, then print “Yes”. Otherwise, print “No”.
Examples:
Input: X = 100, Y = 40021
Output: Yes
Explanation:
Below are the operations performed to convert X into Y:
Operation 1: Multiply X(= 100) by 2, modifies the value X to 200.
Operation 2: Append 1 at the end of X(= 200), modifies the value X to 2001.
Operation 3: Multiply X(= 2001) by 2, modifies the value X to 4002.
Operation 4: Append 1 at the end of X(= 4002), modifies the value X to 40021.
Therefore, from the above operations, it can be seen that the value X can be converted into Y. Hence, print Yes.
Input: X = 17 and Y = 35
Output: No
Approach: The given problem can be solved by performing the operations in the reverse way i.e., try to convert the value Y into X. Follow the steps below to solve the problem:
- Iterate until the value of Y is greater than X and perform the following steps:
- After completing the above steps, if the value of Y is the same as the value of X, then print Yes. Otherwise, print No.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void convertXintoY( int X, int Y)
{
while (Y > X) {
if (Y % 2 == 0)
Y /= 2;
else if (Y % 10 == 1)
Y /= 10;
else
break ;
}
if (X == Y)
cout << "Yes" ;
else
cout << "No" ;
}
int main()
{
int X = 100, Y = 40021;
convertXintoY(X, Y);
return 0;
}
|
Java
import java.util.*;
class GFG{
static void convertXintoY( int X, int Y)
{
while (Y > X) {
if (Y % 2 == 0 )
Y /= 2 ;
else if (Y % 10 == 1 )
Y /= 10 ;
else
break ;
}
if (X == Y)
System.out.print( "Yes" );
else
System.out.print( "No" );
}
public static void main(String[] args)
{
int X = 100 , Y = 40021 ;
convertXintoY(X, Y);
}
}
|
Python3
def convertXintoY(X, Y):
while (Y > X):
if (Y % 2 = = 0 ):
Y / / = 2
elif (Y % 10 = = 1 ):
Y / / = 10
else :
break
if (X = = Y):
print ( "Yes" )
else :
print ( "No" )
if __name__ = = '__main__' :
X,Y = 100 , 40021
convertXintoY(X, Y)
|
C#
using System;
class GFG{
static void convertXintoY( int X, int Y)
{
while (Y > X)
{
if (Y % 2 == 0)
Y /= 2;
else if (Y % 10 == 1)
Y /= 10;
else
break ;
}
if (X == Y)
Console.Write( "Yes" );
else
Console.Write( "No" );
}
public static void Main(String[] args)
{
int X = 100, Y = 40021;
convertXintoY(X, Y);
}
}
|
Javascript
<script>
function convertXintoY(X, Y)
{
while (Y > X) {
if (Y % 2 == 0)
Y = parseInt(Y / 2);
else if (Y % 10 == 1)
Y = parseInt(Y /= 10);
else
break ;
}
if (X == Y)
document.write( "Yes" );
else
document.write( "No" );
}
let X = 100, Y = 40021;
convertXintoY(X, Y);
</script>
|
Time Complexity: log(Y)
Auxiliary Space: O(1)
Recursive Approach:
- Start with X as the initial number.
- Implement a recursive function that takes X and Y as parameters.
- In base case, If X == Y, return True and If X > Y, return False.
- In recursive case, Make two recursive calls with X * 2 and X * 10 + 1 as the new values of X and If either of the recursive calls returns True, return True.
- If no recursive call returns True, return False.
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
bool is_possible( int X, int Y) {
if (X == Y) {
return true ;
}
if (X > Y) {
return false ;
}
return is_possible(X * 2, Y) || is_possible(X * 10 + 1, Y);
}
int main() {
int X = 100;
int Y = 40021;
cout << (is_possible(X, Y) ? "True" : "False" ) << endl;
return 0;
}
|
Java
public class GFG {
public static boolean isPossible( int X, int Y) {
if (X == Y) {
return true ;
}
if (X > Y) {
return false ;
}
return isPossible(X * 2 , Y) || isPossible(X * 10 + 1 , Y);
}
public static void main(String[] args) {
int X = 100 ;
int Y = 40021 ;
System.out.println(isPossible(X, Y) ? "True" : "False" );
}
}
|
Python
def is_possible(X, Y):
if X = = Y:
return True
if X > Y:
return False
return is_possible(X * 2 , Y) or is_possible(X * 10 + 1 , Y)
X = 100
Y = 40021
print (is_possible(X, Y))
|
C#
using System;
public class GFG
{
public static bool IsPossible( int X, int Y)
{
if (X == Y)
{
return true ;
}
if (X > Y)
{
return false ;
}
return IsPossible(X * 2, Y) || IsPossible(X * 10 + 1, Y);
}
public static void Main()
{
int X = 100;
int Y = 40021;
Console.WriteLine(IsPossible(X, Y) ? "True" : "False" );
}
}
|
Javascript
function is_possible(X, Y) {
if (X == Y) {
return true ;
}
if (X > Y) {
return false ;
}
return is_possible(X * 2, Y) || is_possible(X * 10 + 1, Y);
}
let X = 100;
let Y = 40021;
if (is_possible(X, Y))
console.log( "True" );
else
console.log( "False" );
|
Time Complexity: O(2^N)
Auxiliary Space: O(log2(Y/X))
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...