# 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 minimum number of given operation. 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

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

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 ` `using` `namespace` `std; ` ` `  `// Function to return the minimum ` `// operations required ` `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 ` `int` `main() ` `{ ` `    ``int` `n = 120, m = 51840; ` `    ``cout << minOperations(n, m); ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java implementation of the approach ` `class` `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 required ` `def` `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 code ` `if` `__name__ ``=``=` `'__main__'``: ` `    ``n ``=` `120` `    ``m ``=` `51840` `    ``print``(minOperations(n, m)) ` `     `  `# This code is contributed by ` `# Surendra_Gangwar `

## C#

 `// C# implementation of the approach ` `using` `System; ` ` `  `class` `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() ` `{ ` `    ``int` `n = 120, m = 51840; ` `    ``Console.WriteLine(minOperations(n, m)); ` `} ` `} ` ` `  `// This code is contributed  ` `// by Akanksha Rai `

## PHP

 ` `

Output:

```7
``` My Personal Notes arrow_drop_up Check out this Author's contributed articles.

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.