Given a initial number x and two operations which are given below:
- Multiply number by 2.
- Subtract 1 from the number.
The task is to find out minimum number of operation required to convert number x into y using only above two operations. We can apply these operations any number of times.
1 <= x, y <= 10000
Input : x = 4, y = 7 Output : 2 We can transform x into y using following two operations. 1. 4*2 = 8 2. 8-1 = 7 Input : x = 2, y = 5 Output : 4 We can transform x into y using following four operations. 1. 2*2 = 4 2. 4-1 = 3 3. 3*2 = 6 4. 6-1 = 5 Answer = 4 Note that other sequences of two operations would take more operations.
The idea is to use BFS for this. We run a BFS and create nodes by multiplying with 2 and subtracting by 1, thus we can obtain all possible numbers reachable from starting number.
Important Points :
1) When we subtract 1 from a number and if it becomes < 0 i.e. Negative then there is no reason to create next node from it (As per input constraints, numbers x and y are positive).
2) Also, if we have already created a number then there is no reason to create it again. i.e. we maintain a visited array.
This article is contributed by Vipin Khushu. If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
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.
- Minimum number of given operations required to convert a permutation into an identity permutation
- Minimum number of pairs required to make two strings same
- Minimum number of colors required to color a graph
- Minimum number of swaps required to sort an array
- Minimum number of given operations required to make two strings equal
- Minimum steps required to convert X to Y where a binary matrix represents the possible conversions
- Minimize the number of steps required to reach the end of the array | Set 2
- Minimum time required to rot all oranges
- Minimum number of edges between two vertices of a graph using DFS
- Minimum number of edges between two vertices of a Graph
- Minimum steps required to reach the end of a matrix | Set 2
- Minimum number of edges that need to be added to form a triangle
- Find the minimum number of moves to reach end of the array
- Minimum time required to fill the entire matrix with 1's
- Minimum edges required to add to make Euler Circuit
- Min steps to convert N-digit prime number into another by replacing a digit in each step
- Minimum number of prefix reversals to sort permutation of first N numbers
- Minimum colors required such that edges forming cycle do not have same color
- Find the minimum number of moves needed to move from one cell of matrix to another
- Minimum edges required to make a Directed Graph Strongly Connected