Minimum number of given operation required to convert n to m

Given two integers n and m, in a single operation n can be multiplied by either 2 or 3. 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Â
Output: 7Â
120 * 2 * 2 * 2 * 2 * 3 * 3 * 3 = 51840
Input: n = 42, m = 42Â
Output: 0Â
No operation required.
Input: n = 48, m = 72Â
Output: -1Â
Â
Â
Approach: If m is not divisible by n then print -1 as n cannot be converted to m with the given operation. Else we can check if, on dividing, the quotient has only 2 and 3 as prime factors. If yes then the result will be the sum of powers of 2 and 3 else print -1
Below is the implementation of the above approach:Â
Â
C++
// C++ implementation of the approach#include <bits/stdc++.h>using namespace std;Â
// Function to return the minimum// operations requiredint minOperations(int n, int m){Â Â Â Â if (m % n != 0)Â Â Â Â Â Â Â Â return -1;Â
    int minOperations = 0;    int q = m / n;Â
    // Counting all 2s    while (q % 2 == 0) {        q = q / 2;        minOperations++;    }Â
    // Counting all 3s    while (q % 3 == 0) {        q = q / 3;        minOperations++;    }Â
    // If q contained only 2 and 3    // as the only prime factors    // then it must be 1 now    if (q == 1)        return minOperations;Â
    return -1;}Â
// Driver codeint main(){Â Â Â Â int n = 120, m = 51840;Â Â Â Â cout << minOperations(n, m);Â
    return 0;} |
Java
// Java implementation of the approachclass GfG {Â
    // Function to return the minimum    // operations required    static int minOperations(int n, int m)    {        if (m % n != 0)            return -1;Â
        int minOperations = 0;        int q = m / n;Â
        // Counting all 2s        while (q % 2 == 0) {            q = q / 2;            minOperations++;        }Â
        // Counting all 3s        while (q % 3 == 0) {            q = q / 3;            minOperations++;        }Â
        // If q contained only 2 and 3        // as the only prime factors        // then it must be 1 now        if (q == 1)            return minOperations;Â
        return -1;    }Â
    // Driver code    public static void main(String[] args)    {        int n = 120, m = 51840;        System.out.println(minOperations(n, m));    }} |
Python3
# Python 3 implementation of the approachÂ
# Function to return the minimum# operations requireddef minOperations(n, m):Â Â Â Â if (m % n != 0):Â Â Â Â Â Â Â Â return -1Â
    minOperations = 0    q = int(m / n)Â
    # Counting all 2s    while (q % 2 == 0):        q = int(q / 2)        minOperations += 1Â
    # Counting all 3s    while (q % 3 == 0):        q = int(q / 3)        minOperations += 1Â
    # If q contained only 2 and 3    # as the only prime factors    # then it must be 1 now    if (q == 1):        return minOperationsÂ
    return -1Â
# Driver codeif __name__ == '__main__':Â Â Â Â n = 120Â Â Â Â m = 51840Â Â Â Â print(minOperations(n, m))Â Â Â Â Â # This code is contributed by# Surendra_Gangwar |
C#
// C# implementation of the approachusing System;Â
class GFG {Â
// Function to return the minimum// operations requiredstatic int minOperations(int n, int m){Â Â Â Â if (m % n != 0)Â Â Â Â Â Â Â Â return -1;Â
    int minOperations = 0;    int q = m / n;Â
    // Counting all 2s    while (q % 2 == 0)    {        q = q / 2;        minOperations++;    }Â
    // Counting all 3s    while (q % 3 == 0)    {        q = q / 3;        minOperations++;    }Â
    // If q contained only 2 and 3    // as the only prime factors    // then it must be 1 now    if (q == 1)        return minOperations;Â
    return -1;}Â
// Driver codepublic static void Main(){Â Â Â Â int n = 120, m = 51840;Â Â Â Â Console.WriteLine(minOperations(n, m));}}Â
// This code is contributed // by Akanksha Rai |
PHP
<?php// PHP implementation of the approachÂ
// Function to return the minimum// operations requiredfunction minOperations($n, $m){Â Â Â Â if ($m % $n != 0)Â Â Â Â Â Â Â Â return -1;Â
    $minOperations = 0;    $q = $m / $n;Â
    // Counting all 2s    while ($q % 2 == 0)     {        $q = $q / 2;        $minOperations++;    }Â
    // Counting all 3s    while ($q % 3 == 0)     {        $q = $q / 3;        $minOperations++;    }Â
    // If q contained only 2 and 3    // as the only prime factors    // then it must be 1 now    if ($q == 1)        return $minOperations;Â
    return -1;}Â
// Driver code$n = 120; $m = 51840;echo(minOperations($n, $m));Â
// This code is contributed by Code_Mech?> |
Javascript
<script>// javascript implementation of the approachÂ
    // Function to return the minimum    // operations required    function minOperations(n , m) {        if (m % n != 0)            return -1;Â
        var minOperations = 0;        var q = m / n;Â
        // Counting all 2s        while (q % 2 == 0) {            q = q / 2;            minOperations++;        }Â
        // Counting all 3s        while (q % 3 == 0) {            q = q / 3;            minOperations++;        }Â
        // If q contained only 2 and 3        // as the only prime factors        // then it must be 1 now        if (q == 1)            return minOperations;Â
        return -1;    }Â
    // Driver code             var n = 120, m = 51840;        document.write(minOperations(n, m));Â
// This code contributed by Rajput-Ji </script> |
7
Â
Time Complexity: O(log(m/n))
Auxiliary Space: O(1)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!



