Find the minimum number of steps to reach M from N | Java program to find minimum number of steps to reach M from N ; Function to find a minimum number of steps to reach M from N ; Continue till m is greater than n ; If m is odd ; add one ; divide m by 2 ; Return the required answer ; Driver code
class CFG { static int Minsteps ( int n , int m ) { int ans = 0 ; while ( m > n ) { if ( m % 2 != 0 ) { m ++ ; ans ++ ; } m /= 2 ; ans ++ ; } return ans + n - m ; } public static void main ( String [ ] args ) { int n = 4 , m = 6 ; System . out . println ( Minsteps ( n , m ) ) ; } }