Minimum number of operation required to convert number x into y
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.
Constraints:
1 <= x, y <= 10000
Example:
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.
C++
// C++ program to find minimum number of steps needed // to covert a number x into y with two operations // allowed : (1) multiplication with 2 (2) subtraction // with 1. #include<bits/stdc++.h> using namespace std; // A node of BFS traversal struct node { int val; int level; }; // Returns minimum number of operations // needed to covert x into y using BFS int minOperations(int x, int y) { // To keep track of visited numbers // in BFS. set<int> visit; // Create a queue and enqueue x into it. queue<node> q; node n = {x, 0}; q.push(n); // Do BFS starting from x while (!q.empty()) { // Remove an item from queue node t = q.front(); q.pop(); // If the removed item is target // number y, return its level if (t.val == y) return t.level; // Mark dequeued number as visited visit.insert(t.val); // If we can reach y in one more step if (t.val*2 == y || t.val-1 == y) return t.level+1; // Insert children of t if not visited // already if (visit.find(t.val*2) == visit.end()) { n.val = t.val*2; n.level = t.level+1; q.push(n); } if (t.val-1>=0 && visit.find(t.val-1) == visit.end()) { n.val = t.val-1; n.level = t.level+1; q.push(n); } } } // Driver code int main() { int x = 4, y = 7; cout << minOperations(x, y); return 0; } |
Java
// Java program to find minimum // number of steps needed to // covert a number x into y // with two operations allowed : // (1) multiplication with 2 // (2) subtraction with 1. import java.util.HashSet; import java.util.LinkedList; import java.util.Set; class GFG { int val; int steps; public GFG(int val, int steps) { this.val = val; this.steps = steps; } } public class GeeksForGeeks { private static int minOperations(int src, int target) { Set<GFG> visited = new HashSet<>(1000); LinkedList<GFG> queue = new LinkedList<GFG>(); GFG node = new GFG(src, 0); queue.offer(node); visited.add(node); while (!queue.isEmpty()) { GFG temp = queue.poll(); visited.add(temp); if (temp.val == target) { return temp.steps; } int mul = temp.val * 2; int sub = temp.val - 1; // given constraints if (mul > 0 && mul < 1000) { GFG nodeMul = new GFG(mul, temp.steps + 1); queue.offer(nodeMul); } if (sub > 0 && sub < 1000) { GFG nodeSub = new GFG(sub, temp.steps + 1); queue.offer(nodeSub); } } return -1; } // Driver code public static void main(String[] args) { // int x = 2, y = 5; int x = 4, y = 7; GFG src = new GFG(x, y); System.out.println(minOperations(x, y)); } } // This code is contributed by Rahul |
Output :
2
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 contribute@geeksforgeeks.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
Recommended Posts:
- 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
- Minimum time required to rot all oranges
- Minimum edges required to add to make Euler Circuit
- Minimum number of edges between two vertices of a Graph
- Minimum number of edges between two vertices of a graph using DFS
- Minimum number of prefix reversals to sort permutation of first N numbers
- Find the minimum number of moves needed to move from one cell of matrix to another
- Number of Hamiltonian cycle
- Number of Transpositions in a Permutation
- Find the number of islands | Set 1 (Using DFS)
- Number of Unicolored Paths between two nodes
- Number of sink nodes in a graph
- Find the number of Islands | Set 2 (Using Disjoint Set)
Improved By : rahulpopat



