# Reduce a number to 1 by performing given operations

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

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++

`// CPP program to count minimum ` `// steps to reduce a number ` `#include <iostream> ` `#include <cmath> ` ` ` `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*

## 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

## Recommended Posts:

- Number of array elements derivable from D after performing certain operations
- Minimum number operations required to convert n to m | Set-2
- Find minimum number of merge operations to make an array palindrome
- Number of ways to divide a given number as a set of integers in decreasing order
- Total number of possible Binary Search Trees using Catalan Number
- Build Lowest Number by Removing n digits from a given number
- Find the smallest number whose digits multiply to a given number n
- Number of ways to represent a number as sum of k fibonacci numbers
- Find the Number Occurring Odd Number of Times
- Minimum number of cubes whose sum equals to given number N
- Number of subsequences with zero sum
- Happy Number
- Number of subsets with zero sum
- Number of buildings facing the sun
- Find nth Magic Number

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.