Open In App

Minimum steps to convert X to Y by repeated division and multiplication

Given two integers X and Y, the task is to find the minimum number of steps to convert integer X to Y using any of the operations in each step: 

Examples: 



Input: X = 8, Y = 12 
Output:
Explanation: 
First divide 8 by 2: 8/2 = 4 
Then multiply by 3: 4*3 = 12

Input: X = 4, Y = 8 
Output:
Explanation: 
To convert 4 to 8 multiply 4 by 2: 4 * 2 = 8 



Approach: To solve the problem mentioned above: 

If X = 4, Y = 4

Here 4 = 4
Therefore, answer = 0
(as they both are already same)
If X = 4, Y = 12

Here 12 % 4 = 0
Therefore, answer = 1
(4 * 3 = 12)
If X = 8, Y = 13

Here 13 % 8 != 0
Therefore, 
1. X = X/X = 8/8 = 1
2. X = X*Y = 1*13 = 13

Hence, answer = 2

Below is the implementation of the above approach:




// C++ implementation to find minimum
// steps to convert X to Y by repeated
// division and multiplication
 
#include <bits/stdc++.h>
using namespace std;
 
int solve(int X, int Y)
{
    // Check if X is greater than Y
    // then swap the elements
    if (X > Y) {
        int temp = X;
        X = Y;
        Y = temp;
    }
 
    // Check if X equals Y
    if (X == Y)
        cout << 0 << endl;
 
    else if (Y % X == 0)
        cout << 1 << endl;
    else
        cout << 2 << endl;
}
 
// Driver code
int main()
{
    int X = 8, Y = 13;
    solve(X, Y);
 
    return 0;
}




// Java implementation to find minimum
// steps to convert X to Y by repeated
// division and multiplication
class GFG{
 
static int solve(int X, int Y)
{
    // Check if X is greater than Y
    // then swap the elements
    if (X > Y)
    {
        int temp = X;
        X = Y;
        Y = temp;
    }
 
    // Check if X equals Y
    if (X == Y)
        System.out.println(0 );
 
    else if (Y % X == 0)
        System.out.println( 1 );
    else
        System.out.println(2 );
        return 0;
}
 
// Driver code
public static void main(String args[])
{
    int X = 8, Y = 13;
    solve(X, Y);
}
}
 
// This code is contributed by shivanisinghss2110




# Python3 implementation to find minimum
# steps to convert X to Y by repeated
# division and multiplication
def solve(X, Y):
     
    # Check if X is greater than Y
    # then swap the elements
    if (X > Y):
        temp = X
        X = Y
        Y = temp
   
    # Check if X equals Y
    if (X == Y):
        print(0)
   
    elif (Y % X == 0):
        print(1)
    else:
        print(2)
  
# Driver code
X = 8
Y = 13
 
solve(X, Y)
 
# This code is contributed by code_hunt




// C# implementation to find minimum
// steps to convert X to Y by repeated
// division and multiplication
using System;
 
class GFG{
 
static int solve(int X, int Y)
{
     
    // Check if X is greater than Y
    // then swap the elements
    if (X > Y)
    {
        int temp = X;
        X = Y;
        Y = temp;
    }
 
    // Check if X equals Y
    if (X == Y)
        Console.WriteLine(0);
 
    else if (Y % X == 0)
        Console.WriteLine(1);
    else
        Console.WriteLine(2);
    return 0;
}
 
// Driver code
public static void Main(String[] args)
{
    int X = 8, Y = 13;
    solve(X, Y);
}
}
 
// This code is contributed by amal kumar choubey




<script>
 
    // Javascript implementation to find minimum
    // steps to convert X to Y by repeated
    // division and multiplication
     
    function solve(X, Y)
    {
        // Check if X is greater than Y
        // then swap the elements
        if (X > Y) {
            let temp = X;
            X = Y;
            Y = temp;
        }
 
        // Check if X equals Y
        if (X == Y)
            document.write(0);
 
        else if (Y % X == 0)
            document.write(1);
        else
            document.write(2);
    }
     
    let X = 8, Y = 13;
    solve(X, Y);
 
 
</script>

Output
2

Time Complexity: O(1), as we are using only constant-time operations.

Auxiliary Space: O(1), as we are not using any extra space.

Method 2: Use the concept of the Greatest Common Divisor (GCD) of X and Y.

In this approach, we first calculate the GCD of X and Y using the math.gcd() function. Then, we divide X by the GCD to get a reduced value. We then repeatedly divide the reduced value by 2 until we get 1, and count the number of steps taken. Finally, we add 1 to the step count to account for the multiplication step required to convert the reduced value back to Y.




#include <iostream>
#include <bits/stdc++.h>
 
using namespace std;
 
void solve(int X, int Y) {
    int gcd = __gcd(X, Y);
    X /= gcd;
    int steps = 0;
    while (X > 1) {
        X /= 2;
        steps++;
    }
    cout << steps + 1 << endl;
}
 
int main() {
    int X = 8, Y = 12;
    solve(X, Y);
    return 0;
}




import math
 
def solve(X, Y):
    gcd = math.gcd(X, Y)
    X //= gcd
    steps = 0
    while X > 1:
        X //= 2
        steps += 1
    print(steps + 1)
   
X = 8
Y = 12
solve(X, Y)




import java.util.*;
 
public class Main {
    public static void main(String[] args) {
        int X = 8, Y = 12;
        solve(X, Y);
    }
 
    public static void solve(int X, int Y) {
        int gcd = gcd(X, Y);
        X /= gcd;
        int steps = 0;
        while (X > 1) {
            X /= 2;
            steps++;
        }
        System.out.println(steps + 1);
    }
 
    public static int gcd(int a, int b) {
        return b == 0 ? a : gcd(b, a % b);
    }
}




using System;
 
class Program
{
    static void Main(string[] args)
    {
        int X = 8, Y = 12;
        solve(X, Y);
    }
 
    static void solve(int X, int Y)
    {
        int gcd = MathExt.Gcd(X, Y);
        X /= gcd;
        int steps = 0;
        while (X > 1)
        {
            X /= 2;
            steps++;
        }
        Console.WriteLine(steps + 1);
    }
}
 
public static class MathExt
{
    public static int Gcd(int a, int b)
    {
        return b == 0 ? a : Gcd(b, a % b);
    }
}




function solve(X, Y) {
    const gcd = Gcd(X, Y);
    X /= gcd;
    let steps = 0;
    while (X > 1) {
        X /= 2;
        steps++;
    }
    console.log(steps + 1);
}
 
function Gcd(a, b) {
    return b == 0 ? a : Gcd(b, a % b);
}
 
const X = 8, Y = 12;
solve(X, Y);

Output
2

The time complexity of the provided function is O(log(min(X, Y))) because we are performing division by 2 repeatedly until X reaches 1, and the number of times we need to perform this operation is proportional to the logarithm of X.

The auxiliary space of the function is O(1) because we are using a constant amount of memory to store the variables X, Y, gcd, and steps. The space required for the math module is not considered because it is a built-in module and its space is already reserved by the Python interpreter.


Article Tags :