- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures

- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces

# 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 Articles
- 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
- How to find the minimum number of steps needed by knight to reach the destination using C#?
- Find minimum steps required to reach the end of a matrix in C++
- Program to find minimum steps to reach target position by a chess knight in Python
- Program to find number of optimal steps needed to reach destination by baby and giant steps in Python
- C++ program to find minimum number of steps needed to move from start to end
- Program to find minimum cost to reach final index with at most k steps in python
- Program to find minimum number of steps required to catch the opponent in C++
- Program to find minimum number of hops required to reach end position in Python
- Program to find minimum number of buses required to reach final target in python
- Program to find minimum number of vertices to reach all nodes using Python
- Convert a number m to n using minimum number of given operations in C++
- Program to find minimum number of heights to be increased to reach destination in Python
- C++ code to find minimum number starting from n in a game

Advertisements