Given a number N. The task is to reduce the given number N to 1 in the minimum number of steps. You can perform any one of the below operations in each step.

**Operation 1**: If the number is even then you can divide the number by 2.**Operation 2**: If the number is odd then you are allowed to perform either (n+1) or (n-1).

You need to print the minimum number of steps required to reduce the number N to 1 by performing the above operations.**Examples**:

Input: n = 15Output: 5 15 is odd 15+1=16 16 is even 16/2=8 8 is even 8/2=4 4 is even 4/2=2 2 is even 2/2=1Input: n = 7Output: 4 7->6 6->3 3->2 2->1

**Method 1 – **

The idea is to recursively compute the minimum number of steps required.

- If the number is even, then we are allowed to only divide the number by 2.
- But, when the number is Odd, we can either increment or decrement it by 1. So, we will use recursion for both n-1 and n+1 and return the one with the minimum number of operations.

Below is the implementation of above approach:

## C++

`// C++ program to count minimum ` `// steps to reduce a number ` `#include <cmath> ` `#include <iostream> ` ` ` `using` `namespace` `std; ` ` ` `int` `countways(` `int` `n) ` `{ ` ` ` `if` `(n == 1) ` ` ` `return` `0; ` ` ` `else` `if` `(n % 2 == 0) ` ` ` `return` `1 + countways(n / 2); ` ` ` `else` ` ` `return` `1 + min(countways(n - 1), ` ` ` `countways(n + 1)); ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `n = 15; ` ` ` ` ` `cout << countways(n) << ` `"\n"` `; ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to count minimum ` `// steps to reduce a number ` `class` `Geeks { ` ` ` ` ` `static` `int` `countways(` `int` `n) ` ` ` `{ ` ` ` `if` `(n == ` `1` `) ` ` ` `return` `0` `; ` ` ` `else` `if` `(n % ` `2` `== ` `0` `) ` ` ` `return` `1` `+ countways(n / ` `2` `); ` ` ` `else` ` ` `return` `1` `+ Math.min(countways(n - ` `1` `), countways(n + ` `1` `)); ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main(String args[]) ` ` ` `{ ` ` ` `int` `n = ` `15` `; ` ` ` ` ` `System.out.println(countways(n)); ` ` ` `} ` `} ` ` ` `// This code is contributed by ankita_saini ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to count minimum ` `# steps to reduce a number ` ` ` ` ` `def` `countways(n): ` ` ` `if` `(n ` `=` `=` `1` `): ` ` ` `return` `0` `; ` ` ` `elif` `(n ` `%` `2` `=` `=` `0` `): ` ` ` `return` `1` `+` `countways(n ` `/` `2` `); ` ` ` `else` `: ` ` ` `return` `1` `+` `min` `(countways(n ` `-` `1` `), ` ` ` `countways(n ` `+` `1` `)); ` ` ` `# Driver code ` `n ` `=` `15` `; ` `print` `(countways(n)); ` ` ` `# This code is contributed by PrinciRaj1992 ` |

*chevron_right*

*filter_none*

## C#

`// C# program to count minimum ` `// steps to reduce a number ` `using` `System; ` ` ` `class` `GFG { ` ` ` `static` `int` `countways(` `int` `n) ` ` ` `{ ` ` ` `if` `(n == 1) ` ` ` `return` `0; ` ` ` `else` `if` `(n % 2 == 0) ` ` ` `return` `1 + countways(n / 2); ` ` ` `else` ` ` `return` `1 + Math.Min(countways(n - 1), countways(n + 1)); ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `static` `public` `void` `Main() ` ` ` `{ ` ` ` `int` `n = 15; ` ` ` `Console.Write(countways(n)); ` ` ` `} ` `} ` ` ` `// This code is contributed by Raj ` |

*chevron_right*

*filter_none*

**Output:**

5

The above-mentioned approach has a time complexity of O(2^n). It is possible to reduce this complexity to O(log n).

**Method 2 – (Efficient Solution)**

It is clear with little observation that performing an increment of 1 or a decrement of 1 on an odd number can result to an even number, one of it divisible by 4. For an odd number the only operation possible is either an increment of 1 or a decrement of 1, most certainly one operation will result in a number divisible by four, this is the optimal choice clearly.

Algorithm :1. Initialize count = 0 2. While number is greater than one perform following steps - Perform count++ for each iteration if num % 2 == 0, perform division else if num % 4 == 3, perform increment else perform decrement (as odd % 4 is either 1 or 3) 3. return count;

## C++

`// C++ program for the above approach ` `#include <iostream> ` `using` `namespace` `std; ` ` ` `int` `countSteps(` `int` `n) ` `{ ` ` ` `int` `count = 0; ` ` ` `while` `(n > 1) { ` ` ` `count++; ` ` ` ` ` `// num even, divide by 2 ` ` ` `if` `(n % 2 == 0) ` ` ` `n /= 2; ` ` ` ` ` `// num odd, n%4 == 1, decrement by 1 ` ` ` `else` `if` `(n % 4 == 1) ` ` ` `n -= 1; ` ` ` ` ` `// num odd, n%4 == 3, increment by 1 ` ` ` `else` ` ` `n += 1; ` ` ` `} ` ` ` ` ` `return` `count; ` `} ` ` ` `// driver code ` ` ` `int` `main() ` `{ ` ` ` `int` `n = 15; ` ` ` ` ` `// Function call ` ` ` `cout << countSteps(n) << ` `"\n"` `; ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program for the above approach ` `def` `countSteps(n): ` ` ` ` ` `count ` `=` `0` ` ` `while` `(n > ` `1` `): ` ` ` `count ` `+` `=` `1` ` ` ` ` `# num even, divide by 2 ` ` ` `if` `(n ` `%` `2` `=` `=` `0` `): ` ` ` `n ` `/` `/` `=` `2` ` ` ` ` `# num odd, n%4 == 1, decrement by 1 ` ` ` `elif` `(n ` `%` `4` `=` `=` `1` `): ` ` ` `n ` `-` `=` `1` ` ` ` ` `# num odd, n%4 == 3, increment by 1 ` ` ` `else` `: ` ` ` `n ` `+` `=` `1` ` ` ` ` `return` `count ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `n ` `=` `15` ` ` ` ` `# Function call ` ` ` `print` `(countSteps(n)) ` ` ` `# This code is contributed by chitranayal ` |

*chevron_right*

*filter_none*

**Output:**

5

**Time complexity :** O( logN )

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:

- Maximum score possible after performing given operations on an Array
- Maximum count of values of S modulo M lying in a range [L, R] after performing given operations on the array
- Minimize Cost to reduce the Array to a single element by given operations
- Minimum steps to reduce N to 0 by given operations
- Number of array elements derivable from D after performing certain operations
- Min number of operations to reduce N to 0 by subtracting any digits from N
- Minimum number of operations required to reduce N to 0
- Check if K can be obtained by performing arithmetic operations on any permutation of an Array
- Minimize given flips required to reduce N to 0
- Count number of ways to convert string S to T by performing K cyclic shifts
- Maximum number of given operations to remove the entire string
- Number from a range [L, R] having Kth minimum cost of conversion to 1 by given operations
- Maximum Length of Sequence of Sums of prime factors generated by the given operations
- Kth character from the Nth string obtained by the given operations
- Mth bit in Nth binary string from a sequence generated by the given operations
- Minimum Steps to obtain N from 1 by the given operations
- Find minimum number of merge operations to make an array palindrome
- Minimum number operations required to convert n to m | Set-2
- Minimize operations required to obtain N
- Minimum number of substrings the given string can be splitted into that satisfy the given conditions

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.