Sunday, September 22, 2024
Google search engine
HomeData Modelling & AIMinimum steps to reach N from 1 by multiplying each step by...

Minimum steps to reach N from 1 by multiplying each step by 2, 3, 4 or 5

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:
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 Algorithm 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 = 10
print(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


Javascript




<script>
 
// Javascript 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
function Minsteps(n)
{
    var 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
var n = 10;
 
// Function Call
document.write(Minsteps(n));
 
// This code is contributed by Khushboogoyal499
    
</script>


Output: 

2

 

Time Complexity: O(log n)

Auxiliary Space: O(1)

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

Most Popular

Recent Comments