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
Input: X = 8, Y = 12
First divide 8 by 2: 8/2 = 4
Then multiply by 3: 4*3 = 12
Input: X = 4, Y = 8
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.
If X = 4, Y = 4 Here 4 = 4 Therefore, answer = 0 (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.
If X = 4, Y = 12 Here 12 % 4 = 0 Therefore, answer = 1 (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).
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:
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.
- Write you own Power without using multiplication(*) and division(/) operators
- Divide two integers without using multiplication, division and mod operator | Set2
- Multiply two integers without using multiplication, division and bitwise operators, and no loops
- Convert 1 into X in min steps by multiplying with 2 or 3 or by adding 1
- Number of steps to convert to prime factors
- Find minimum number K such that sum of array after multiplication by K exceed S
- Count minimum steps to get the given desired array
- Minimum steps to color the tree with given colors
- Find the minimum number of steps to reach M from N
- Minimum steps to reach N from 1 by multiplying each step by 2, 3, 4 or 5
- Generate array with minimum sum which can be deleted in P steps
- Count the minimum steps to reach 0 from the given integer N
- Minimum steps required to reduce all the elements of the array to zero
- Minimum steps for increasing and decreasing Array to reach either 0 or N
- Minimum steps to make the product of the array equal to 1
- Minimum steps to make sum and the product of all elements of array non-zero
- Minimum number of steps required to obtain the given Array by the given operations
- Minimum steps to reach the Nth stair in jumps of perfect power of 2
- Minimum steps to come back to starting point in a circular tour
- Min steps to convert N-digit prime number into another by replacing a digit in each step
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.