Given a positive number N, we need to reach to 1 in minimum number of steps where a step is defined as converting N to (N-1) or converting N to its one of the bigger divisor.
Formally, if we are at N, then in 1 step we can reach to (N – 1) or if N = u*v then we can reach to max(u, v) where u > 1 and v > 1.
Input : N = 17 Output : 4 We can reach to 1 in 4 steps as shown below, 17 -> 16(from 17 - 1) -> 4(from 4 * 4) -> 2(from 2 * 2) -> 1(from 2 - 1) Input : N = 50 Output : 5 We can reach to 1 in 5 steps as shown below, 50 -> 10(from 5 * 10) -> 5(from 2 * 5) -> 4(from 5 - 1) -> 2(from 2 *2) -> 1(from 2 - 1)
We can solve this problem using breadth first search because it works level by level so we will reach to 1 in minimum number of steps where next level for N contains (N – 1) and bigger proper factors of N.
Complete BFS procedure will be as follows, First we will push N with steps 0 into the data queue then at each level we will push their next level elements with 1 step more than its previous level elements. In this way when 1 will be popped out from queue, it will contain minimum number of steps with it, which will be our final result.
In below code a queue of a structure of ‘data’ type is used which stores value and steps from N in it, another set of integer type is used to save ourselves from pushing the same element more than once which can lead to an infinite loop. So at each step, we push the value into set after pushing that into the queue so that the value won’t be visited more than once.
Please see below code for better understanding,
This article is contributed by Utkarsh Trivedi. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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 steps to reach N from 1 by multiplying each step by 2, 3, 4 or 5
- Reach A and B by multiplying them with K and K^2 at every step
- Find the number of ways to reach Kth step in stair case
- Count ways to reach the Nth stair using multiple 1 or 2 steps and a single step 3
- Minimize steps to reach K from 0 by adding 1 or doubling at each step
- Shortest path to reach one prime to other by changing single digit at a time
- Steps to reduce N to zero by subtracting its most significant digit at every step
- Delete odd and even numbers at alternate step such that sum of remaining elements is minimized
- Number of steps to sort the array by changing order of three elements in each step
- Min steps to convert N-digit prime number into another by replacing a digit in each step
- Count of prime factors of N to be added at each step to convert N to M
- Minimum number of jumps to reach end
- Minimum steps to reach end of array under constraints
- Minimum steps to reach target by a Knight | Set 1
- Minimum cells traversed to reach corner where every cell represents jumps
- Find minimum moves to reach target on an infinite line
- Minimum steps to reach any of the boundary edges of a matrix | Set-2
- Minimum steps to reach end from start by performing multiplication and mod operations with array elements
- Minimum time to reach a point with +t and -t moves at time t
- Minimum cost to reach a point N from 0 with two different operations allowed
Improved By : 29AjayKumar