Skip to content
Related Articles

Related Articles

Improve Article

Convert X into Y by repeatedly multiplying X with 2 or appending 1 at the end

  • Last Updated : 24 Jun, 2021

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++




// C++ program for the above approach
  
#include <bits/stdc++.h>
using namespace std;
  
// Function to check if X can be
// converted to Y by multiplying
// X by 2 or appending 1 at the end
void convertXintoY(int X, int Y)
{
    // Iterate until Y is at least X
    while (Y > X) {
  
        // If Y is even
        if (Y % 2 == 0)
            Y /= 2;
  
        // If the last digit of Y is 1
        else if (Y % 10 == 1)
            Y /= 10;
  
        // Otherwise
        else
            break;
    }
  
    // Check if X is equal to Y
    if (X == Y)
        cout << "Yes";
    else
        cout << "No";
}
  
// Driver Code
int main()
{
    int X = 100, Y = 40021;
    convertXintoY(X, Y);
  
    return 0;
}

Java




// Java program for the above approach
import java.util.*;
  
class GFG{
  
// Function to check if X can be
// converted to Y by multiplying
// X by 2 or appending 1 at the end
static void convertXintoY(int X, int Y)
{
    // Iterate until Y is at least X
    while (Y > X) {
  
        // If Y is even
        if (Y % 2 == 0)
            Y /= 2;
  
        // If the last digit of Y is 1
        else if (Y % 10 == 1)
            Y /= 10;
  
        // Otherwise
        else
            break;
    }
  
    // Check if X is equal to Y
    if (X == Y)
        System.out.print("Yes");
    else
        System.out.print("No");
}
  
// Driver Code
public static void main(String[] args)
{
    int X = 100, Y = 40021;
    convertXintoY(X, Y);
}
}
  
// This code is contributed by sanjoy_62.

Python3




# Python program for the above approach
  
# Function to check if X can be
# converted to Y by multiplying
# X by 2 or appending 1 at the end
def convertXintoY(X, Y):
    # Iterate until Y is at least X
    while (Y > X):
  
        # If Y is even
        if (Y % 2 == 0):
            Y //= 2
  
        # If the last digit of Y is 1
        elif (Y % 10 == 1):
            Y //= 10
  
        # Otherwise
        else:
            break
  
    # Check if X is equal to Y
    if (X == Y):
        print("Yes")
    else:
        print("No")
  
# Driver Code
if __name__ == '__main__':
    X,Y = 100, 40021
    convertXintoY(X, Y)
  
# This code is contributed by mohit kumar 29.

C#




// C# program for the above approach
using System;
  
class GFG{
  
// Function to check if X can be
// converted to Y by multiplying
// X by 2 or appending 1 at the end
static void convertXintoY(int X, int Y)
{
      
    // Iterate until Y is at least X
    while (Y > X) 
    {
          
        // If Y is even
        if (Y % 2 == 0)
            Y /= 2;
  
        // If the last digit of Y is 1
        else if (Y % 10 == 1)
            Y /= 10;
  
        // Otherwise
        else
            break;
    }
  
    // Check if X is equal to Y
    if (X == Y)
        Console.Write("Yes");
    else
        Console.Write("No");
}
  
// Driver Code
public static void Main(String[] args)
{
    int X = 100, Y = 40021;
      
    convertXintoY(X, Y);
}
}
  
// This code is contributed by shivanisinghss2110

Javascript




<script>
// JavaScript program for the above approach
  
        // Function to check if X can be
        // converted to Y by multiplying
        // X by 2 or appending 1 at the end
        function convertXintoY(X, Y)
        {
            // Iterate until Y is at least X
            while (Y > X) {
  
                // If Y is even
                if (Y % 2 == 0)
                    Y = parseInt(Y / 2);
  
                // If the last digit of Y is 1
                else if (Y % 10 == 1)
                    Y = parseInt(Y /= 10);
  
                // Otherwise
                else
                    break;
            }
  
            // Check if X is equal to Y
            if (X == Y)
                document.write("Yes");
            else
                document.write("No");
        }
  
        // Driver Code
        let X = 100, Y = 40021;
        convertXintoY(X, Y);
  
  // This code is contributed by Potta Lokesh
    </script>
Output: 
Yes

 

Time Complexity: log(Y)
Auxiliary Space: O(1)

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :