Minimum number of given operation required to convert n to m | C ++ implementation of the approach ; Function to return the minimum operations required ; Counting all 2 s ; Counting all 3 s ; If q contained only 2 and 3 as the only prime factors then it must be 1 now ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minOperations ( int n , int m ) { if ( m % n != 0 ) return -1 ; int minOperations = 0 ; int q = m / n ; while ( q % 2 == 0 ) { q = q / 2 ; minOperations ++ ; } while ( q % 3 == 0 ) { q = q / 3 ; minOperations ++ ; } if ( q == 1 ) return minOperations ; return -1 ; } int main ( ) { int n = 120 , m = 51840 ; cout << minOperations ( n , m ) ; return 0 ; }