- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Find the minimum number of steps to reach M from N in C++

Suppose we have two integers N and M. We have to find minimum number of steps to reach M from N, by performing given operations −

- Multiply the number x by 2, so x will be 2*x
- Subtract one from the number x, so the number will be x – 1

If N = 4 and M = 6, then output will be 2. So if we perform operation number 2 on N, then N becomes 3, then perform operation number one on updated value of N, so it becomes 2 * 3 = 6. So the minimum number of steps will be 2.

To solve this problem, we will follow these rules −

- We can reverse the problem, like we take the number N starting from M, so new two operations will be
- Divide the number by 2, when it is even,
- add 1 with the number

- Now the minimum number of operations will be
- if N > M, return the difference between them, so number of steps will be adding 1 to M, until it becomes equal to N
- Otherwise when N < M, keep dividing M by 2, until it becomes less than N. If M is odd, then add 1 to it first, 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.

## Example

#include<iostream> using namespace std; int countMinimumSteps(int n, int m) { int count = 0; while(m > n) { if(m % 2 == 1) { m++; count++; } m /= 2; count++; } return count + n - m; } int main() { int n = 4, m = 6; cout << "Minimum number of operations required: " << countMinimumSteps(n, m); }

## Output

Minimum number of operations required: 2

- Related Questions & Answers
- Finding minimum number of required operations to reach n from m in JavaScript
- Program to find number of minimum steps to reach last index in Python
- Find minimum steps required to reach the end of a matrix in C++
- How to find the minimum number of steps needed by knight to reach the destination using C#?
- Program to find number of optimal steps needed to reach destination by baby and giant steps in Python
- Convert a number m to n using minimum number of given operations in C++
- C++ program to find minimum number of steps needed to move from start to end
- Program to find minimum number of steps required to catch the opponent in C++
- Program to find minimum steps to reach target position by a chess knight in Python
- Find a positive number M such that gcd(N^M,N&M) is maximum in Python
- Sum of even numbers from n to m regardless if n<m or n>m JavaScript
- Program to find minimum cost to reach final index with at most k steps in python
- Minimum and Maximum number of pairs in m teams of n people in C++
- Program to find minimum number of vertices to reach all nodes using Python
- Find the number closest to n and divisible by m in C++

Advertisements