Given an integer N, the task is to find the minimum number of steps to reach the number N from 1 by multiplying each step by 2, 3, 4 or 5. If it is not possible to reach N, print -1.
Examples:
Input: N = 10
Output: 2
Explanation:
Initial number = 1
Step 1: Multiply it by 2, Current Number = 2
Step 2: Multiply it by 5, Current Number = 10
Therefore, Minimum 2 steps required to reach 10.Input: N = 13
Output: -1
Explanation:
There is no way reach 13 using any given operations
Approach: The idea is to use Greedy Alogorithm to choose the operation that should be performed at each step and perform the operations in the reverse manner that is instead of going from 1 to N, find the operations required to reach N to 1. Below is the illustration of the steps:
- Apply the operations below until N is greater than 1.
- Check if N is divisible by 5, Then increase steps by 1 and reduce N to N/5
- Else, check if N is divisible by 4, Then increase steps by 1 and reduce N to N/4
- Else, check if N is divisible by 3, Then increase steps by 1 and reduce N to N/3
- Else, check if N is divisible by 2, Then increase steps by 1, and reduce N to N/2
- If at any step no operation can be applied then there is no possible set of operations to reach N from 1. Therefore, return -1.
Below is the implementation of the above approach:
C++
// C++ implementation to find // minimum number of steps // to reach N from 1 #include <bits/stdc++.h> using namespace std; // Function to find a minimum number // of steps to reach N from 1 int Minsteps(int n) { int ans = 0; // Check until N is greater // than 1 and operations // can be applied while (n > 1) { // Condition to choose the // operations greedily if (n % 5 == 0) { ans++; n = n / 5; continue; } else if (n % 4 == 0) { ans++; n = n / 4; continue; } else if (n % 3 == 0) { ans++; n = n / 3; continue; } else if (n % 2 == 0) { ans++; n = n / 2; continue; } return -1; } return ans; } // Driver code int main() { int n = 10; cout << Minsteps(n); return 0; } |
Java
// Java implementation to find // minimum number of steps // to reach N from 1 import java.util.*; class GFG{ // Function to find a minimum number // of steps to reach N from 1 static int Minsteps(int n) { int ans = 0; // Check until N is greater // than 1 and operations // can be applied while (n > 1) { // Condition to choose the // operations greedily if (n % 5 == 0) { ans++; n = n / 5; continue; } else if (n % 4 == 0) { ans++; n = n / 4; continue; } else if (n % 3 == 0) { ans++; n = n / 3; continue; } else if (n % 2 == 0) { ans++; n = n / 2; continue; } return -1; } return ans; } // Driver code public static void main(String[] args) { int n = 10; System.out.print(Minsteps(n)); } } // This code is contributed by Amit Katiyar |
Python3
# Python3 implementation to find # minimum number of steps # to reach N from 1 # Function to find a minimum number # of steps to reach N from 1 def Minsteps(n): ans = 0 # Check until N is greater # than 1 and operations # can be applied while (n > 1): # Condition to choose the # operations greedily if (n % 5 == 0): ans = ans + 1 n = n / 5 continue elif (n % 4 == 0): ans = ans + 1 n = n / 4 continue elif (n % 3 == 0): ans = ans + 1 n = n / 3 continue elif (n % 2 == 0): ans = ans + 1 n = n / 2 continue return -1 return ans # Driver code n = 10print(Minsteps(n)) # This code is contributed by Pratik |
C#
// C# implementation to find // minimum number of steps // to reach N from 1 using System; class GFG{ // Function to find a minimum number // of steps to reach N from 1 static int Minsteps(int n) { int ans = 0; // Check until N is greater // than 1 and operations // can be applied while (n > 1) { // Condition to choose the // operations greedily if (n % 5 == 0) { ans++; n = n / 5; continue; } else if (n % 4 == 0) { ans++; n = n / 4; continue; } else if (n % 3 == 0) { ans++; n = n / 3; continue; } else if (n % 2 == 0) { ans++; n = n / 2; continue; } return -1; } return ans; } // Driver code public static void Main() { int n = 10; Console.Write(Minsteps(n)); } } // This code is contributed by rutvik_56 |
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.
Recommended Posts:
- Reach A and B by multiplying them with K and K^2 at every step
- Minimize steps to reach K from 0 by adding 1 or doubling at each step
- Count ways to reach the Nth stair using multiple 1 or 2 steps and a single step 3
- Minimum step to reach one
- Find the minimum number of steps to reach M from N
- Count the minimum steps to reach 0 from the given integer N
- Minimum steps for increasing and decreasing Array to reach either 0 or N
- Minimum steps to reach the Nth stair in jumps of perfect power of 2
- Convert 1 into X in min steps by multiplying with 2 or 3 or by adding 1
- Steps to reduce N to zero by subtracting its most significant digit at every step
- Find the number of ways to reach Kth step in stair case
- Number of steps to sort the array by changing order of three elements in each step
- Check if it is possible to reach (x, y) from origin in exactly Z steps using only plus movements
- Number of steps required to reach point (x,y) from (0,0) using zig-zag way
- Minimize the number of steps required to reach the end of the array | Set 2
- Minimum operations required to convert X to Y by multiplying X with the given co-primes
- Min steps to convert N-digit prime number into another by replacing a digit in each step
- Minimum number of jumps to reach end
- Minimum steps to color the tree with given colors
- Count minimum steps to get the given desired array
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.

