Open In App

Minimum number operations required to convert n to m | Set-2

Improve
Improve
Like Article
Like
Save
Share
Report

Given two integers n and m and a and b, in a single operation n can be multiplied by either a or b. The task is to convert n to m with a minimum number of given operations. If it is impossible to convert n to m with the given operation then print -1.

Examples: 

Input: n = 120, m = 51840, a = 2, b = 3
Output: 7
120 * 2 * 2 * 2 * 2 * 3 * 3 * 3 = 51840

Input: n = 10, m = 50, a = 5, b = 7
Output: 1
10 * 5 = 50 

In the previous post, we discussed an approach using division. 
In this post, we will use an approach that finds the minimum number of operations using recursion. The recursion will consist of two states, the number being multiplied by a or by b, and counting the steps. The minimum of both steps will be the answer. 
 

Below is the implementation of the above approach: 

C++




// C++ implementation of the above approach
 
#include <bits/stdc++.h>
using namespace std;
#define MAXN 10000000
 
// Function to find the minimum number of steps
int minimumSteps(int n, int m, int a, int b)
{
    // If n exceeds M
    if (n > m)
        return MAXN;
 
    // If N reaches the target
    if (n == m)
        return 0;
 
    // The minimum of both the states
    // will be the answer
    return min(1 + minimumSteps(n * a, m, a, b),
               1 + minimumSteps(n * b, m, a, b));
}
 
// Driver code
int main()
{
    int n = 120, m = 51840;
    int a = 2, b = 3;
    cout << minimumSteps(n, m, a, b);
    return 0;
}


Java




// Java implementation of the above approach
class GFG
{
    static int MAXN = 10000000;
     
    // Function to find the minimum number of steps
    static int minimumSteps(int n, int m, int a, int b)
    {
        // If n exceeds M
        if (n > m)
            return MAXN;
     
        // If N reaches the target
        if (n == m)
            return 0;
     
        // The minimum of both the states
        // will be the answer
        return Math.min(1 + minimumSteps(n * a, m, a, b),
                1 + minimumSteps(n * b, m, a, b));
    }
     
    // Driver code
    public static void main (String[] args)
    {
        int n = 120, m = 51840;
        int a = 2, b = 3;
        System.out.println(minimumSteps(n, m, a, b));
    }
}
 
// This code is contributed by ihritik


Python3




# Python 3 implementation of the
# above approach
MAXN = 10000000
 
# Function to find the minimum
# number of steps
def minimumSteps(n, m, a, b):
     
    # If n exceeds M
    if (n > m):
        return MAXN
 
    # If N reaches the target
    if (n == m):
        return 0
 
    # The minimum of both the states
    # will be the answer
    return min(1 + minimumSteps(n * a, m, a, b),
               1 + minimumSteps(n * b, m, a, b))
 
# Driver code
if __name__ == '__main__':
    n = 120
    m = 51840
    a = 2
    b = 3
    print(minimumSteps(n, m, a, b))
 
# This code is contributed by
# Surendra_Gangwar


C#




// C# implementation of the above approach
using System;
 
class GFG
{
    static int MAXN = 10000000;
     
    // Function to find the minimum number of steps
    static int minimumSteps(int n, int m, int a, int b)
    {
        // If n exceeds M
        if (n > m)
            return MAXN;
     
        // If N reaches the target
        if (n == m)
            return 0;
     
        // The minimum of both the states
        // will be the answer
        return Math.Min(1 + minimumSteps(n * a, m, a, b),
                1 + minimumSteps(n * b, m, a, b));
    }
     
    // Driver code
    public static void Main ()
    {
        int n = 120, m = 51840;
        int a = 2, b = 3;
        Console.WriteLine(minimumSteps(n, m, a, b));
    }
}
 
// This code is contributed by ihritik


PHP




<?php
// PHP implementation of the above approach
$MAXN = 10000000;
 
// Function to find the minimum number of steps
function minimumSteps($n, $m, $a, $b)
{
    global $MAXN;
     
    // If n exceeds M
    if ($n > $m)
        return $MAXN;
 
    // If N reaches the target
    if ($n == $m)
        return 0;
 
    // The minimum of both the states
    // will be the answer
    return min(1 + minimumSteps($n * $a, $m, $a, $b),
               1 + minimumSteps($n * $b, $m, $a, $b));
}
 
// Driver code
$n = 120; $m = 51840;
$a = 2; $b = 3;
echo minimumSteps($n, $m, $a, $b);
 
// This code is contributed by Akanksha Rai
?>


Javascript




<script>
// javascript implementation of the above approach
 
    var MAXN = 10000000;
 
    // Function to find the minimum number of steps
    function minimumSteps(n , m , a , b) {
        // If n exceeds M
        if (n > m)
            return MAXN;
 
        // If N reaches the target
        if (n == m)
            return 0;
 
        // The minimum of both the states
        // will be the answer
        return Math.min(1 + minimumSteps(n * a, m, a, b),
        1 + minimumSteps(n * b, m, a, b));
    }
 
    // Driver code
     
        var n = 120, m = 51840;
        var a = 2, b = 3;
        document.write(minimumSteps(n, m, a, b));
 
// This code contributed by Rajput-Ji
</script>


Output: 

7

 

Time Complexity: O(m2)

Auxiliary Space: O(m2 + MAXN)



Last Updated : 09 Jun, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads