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