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

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

• Divide the number by any natural number
• Multiply the number with any natural number

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:

• Make sure X contains the smaller value among X and Y. Now, if X is greater than Y then we know that it is always easier to change a smaller number to a larger number. Hence, we just swap the values of X and Y and then follow the steps mentioned below.
• If both the integers are same then the answer will be zero as no conversion takes place.
For example,

```If X = 4, Y = 4

Here 4 = 4
(as they both are already same)
```
• However, if X is less than Y then:
• we have to check that Y % X gives 0 or not.
• If yes, then Y can be represented as X * (Y / X) and we get the desired output in single-step.
For example,

```If X = 4, Y = 12

Here 12 % 4 = 0
(4 * 3 = 12)
```
• Otherwise, the answer will be 2 as it takes two steps, one for division (X = X/X) and other for multiplication (X = X * Y).
For example,

```If X = 8, Y = 13

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

```

Below is the implementation of the above approach:

## C++

 `// C++ implementation to find minimum ` `// steps to convert X to Y by repeated ` `// division and multiplication ` ` `  `#include ` `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

 `// 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`

## C#

 `// 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 `

Output:

```2
```

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.

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.