Given two integers **N** and **M**. The task is to find the minimum number of steps to reach M from N by performing given operations.

- Multiply a number x by 2. So, x becomes 2*x.
- Subtract one from the number x. So, x becomes x-1.

**Examples:**

Input :N = 4, M = 6Output :2Explanation :Perform operation number 2 on N. So, N becomes 3 and then perform operation number 1. Then, N becomes 6. So, the minimum number of steps is 2.Input :N = 10, M = 1Output :9Explanation :Perform operation number two 9 times on N. Then N becomes 1.

**Approach **:

The idea is to reverse the problem as follows: We should get the number N starting from M using the operations:

- Divide the number by 2 if it is even.
- Add 1 to the number.

Now, the minimum number of operations would be:

- If N > M, return the difference between them, that is, number of steps will be adding 1 to M until it becomes equal to N.
- Else if N < M.
- Keep dividing M by 2 until it becomes less than N. If M is odd, add 1 to it first and then divide by 2. Once M is less than N, add the difference between them to the count along with the count of above operations.

Below is the implementation of the above approach:

## C++

`// CPP program to find minimum number ` `// of steps to reach M from N ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to find a minimum number ` `// of steps to reach M from N ` `int` `Minsteps(` `int` `n, ` `int` `m) ` `{ ` ` ` `int` `ans = 0; ` ` ` ` ` `// Continue till m is greater than n ` ` ` `while` `(m > n) ` ` ` `{ ` ` ` `// If m is odd ` ` ` `if` `(m&1) ` ` ` `{ ` ` ` `// add one ` ` ` `m++; ` ` ` `ans++; ` ` ` `} ` ` ` ` ` `// divide m by 2 ` ` ` `m /= 2; ` ` ` `ans++; ` ` ` `} ` ` ` ` ` `// Return the required answer ` ` ` `return` `ans + n - m; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `n = 4, m = 6; ` ` ` ` ` `cout << Minsteps(n, m); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

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

*chevron_right*

*filter_none*

## Python3

`# Python3 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 ` `def` `Minsteps(n, m): ` ` ` ` ` `ans ` `=` `0` ` ` ` ` `# Continue till m is greater than n ` ` ` `while` `(m > n): ` ` ` ` ` `# If m is odd ` ` ` `if` `(m & ` `1` `): ` ` ` ` ` `# add one ` ` ` `m ` `+` `=` `1` ` ` `ans ` `+` `=` `1` ` ` ` ` `# divide m by 2 ` ` ` `m ` `/` `/` `=` `2` ` ` `ans ` `+` `=` `1` ` ` ` ` `# Return the required answer ` ` ` `return` `ans ` `+` `n ` `-` `m ` ` ` `# Driver code ` `n ` `=` `4` `m ` `=` `6` ` ` `print` `(Minsteps(n, m)) ` ` ` `# This code is contributed by mohit kumar ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find minimum number ` `// of steps to reach M from N ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `// Function to find a minimum number ` `// of steps to reach M from N ` `static` `int` `Minsteps(` `int` `n, ` `int` `m) ` `{ ` ` ` `int` `ans = 0; ` ` ` ` ` `// Continue till m is greater than n ` ` ` `while` `(m > n) ` ` ` `{ ` ` ` `// If m is odd ` ` ` `if` `(m % 2 != 0) ` ` ` `{ ` ` ` `// add one ` ` ` `m++; ` ` ` `ans++; ` ` ` `} ` ` ` ` ` `// divide m by 2 ` ` ` `m /= 2; ` ` ` `ans++; ` ` ` `} ` ` ` ` ` `// Return the required answer ` ` ` `return` `ans + n - m; ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main() ` `{ ` ` ` `int` `n = 4, m = 6; ` ` ` ` ` `Console.WriteLine(Minsteps(n, m)); ` `} ` `} ` ` ` `// This code is contributed ` `// by Akanksha Rai ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP 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 ` `function` `Minsteps(` `$n` `, ` `$m` `) ` `{ ` ` ` `$ans` `= 0; ` ` ` ` ` `// Continue till m is greater than n ` ` ` `while` `(` `$m` `> ` `$n` `) ` ` ` `{ ` ` ` `// If m is odd ` ` ` `if` `(` `$m` `% 2 != 0) ` ` ` `{ ` ` ` `// add one ` ` ` `$m` `++; ` ` ` `$ans` `++; ` ` ` `} ` ` ` ` ` `// divide m by 2 ` ` ` `$m` `/= 2; ` ` ` `$ans` `++; ` ` ` `} ` ` ` ` ` `// Return the required answer ` ` ` `return` `$ans` `+ ` `$n` `- ` `$m` `; ` `} ` ` ` `// Driver code ` `$n` `= 4; ` `$m` `= 6; ` ` ` `echo` `(Minsteps(` `$n` `, ` `$m` `)); ` ` ` `// This code is contributed by Code_Mech ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

2

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Minimum steps to reach the Nth stair in jumps of perfect power of 2
- Minimum steps to reach N from 1 by multiplying each step by 2, 3, 4 or 5
- Count the minimum steps to reach 0 from the given integer N
- Minimum steps for increasing and decreasing Array to reach either 0 or N
- Number of steps required to reach point (x,y) from (0,0) using zig-zag way
- Minimize the number of steps required to reach the end of the array | Set 2
- Count ways to reach the Nth stair using multiple 1 or 2 steps and a single step 3
- Check if it is possible to reach (x, y) from origin in exactly Z steps using only plus movements
- Minimize steps to reach K from 0 by adding 1 or doubling at each step
- Minimize steps defined by a string required to reach the destination from a given source
- Maximum index a pointer can reach in N steps by avoiding a given index B
- Check if it is possible to reach (X, Y) from (1, 0) by given steps
- Check if it is possible to reach (X, Y) from (1, 1) by given steps
- Find the number of jumps to reach X in the number line from zero
- Find minimum moves to reach target on an infinite line
- Find the minimum of maximum length of a jump required to reach the last island in exactly k jumps
- Minimum number of moves required to reach the destination by the king in a chess board
- Minimum number of moves to reach N starting from (1, 1)
- Paths requiring minimum number of jumps to reach end of array
- Minimum number of jumps to reach end

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.