# Reduce a number to 1 by performing given operations | Set 3

Given an integer **N**, the task is to find the number of steps required to reduce the given number **N** to **1** by performing the following operations:

- If the number is a power of 2, then divide the number by
**2**. - Otherwise, subtract the greatest power of 2 smaller than
**N**from**N**.

**Examples:**

Input:N = 2Output:1Explanation:The given number can be reduced to 1 by following the following steps:

Divide the number by 2 as N is a power of 2 which modifies the N to 1.

Therefore, the N can be reduced to 1 in only 1 step.

Input:N = 7Output:2Explanation:The given number can be reduced to 1 by following the following steps:

Subtract 4 the greatest power of 2 less than N. After the step the N modifies to N = 7-4 = 3.

Subtract 2 the greatest power of 2 less than N. After the step the N modifies to N = 3-2 = 1.

Therefore, the N can be reduced to 1 in 2 steps.

**Approach: **The given problem can be solved using bitwise operators. Follow the steps below to solve the problem:

- Initialize two variables say
**res**with value**0**and**MSB**with value**log**to store the number of steps needed to reduce the number to_{2}(N)**1**and the most significant bit of**N**. - Iterate while
**N**is not equal to**1:**- Check if the number is the power of 2 then divide
**N**by**2**. - Otherwise, subtract the largest power of 2 less than N from
**N**i.e update**N**as**N=N-2**^{MSB}. - Increment
**res**by**1**and decrement**MSB**by**1**.

- Check if the number is the power of 2 then divide
- Finally, print
**res**.

Below is the implementation of the above approach:

## C++

`// C++ program for the above approach` `#include <bits/stdc++.h>` `using` `namespace` `std;` `// Function to find steps needed to` `// reduce a given integer to 1` `int` `reduceToOne(` `int` `N)` `{` ` ` `// Stores the most` ` ` `// significant bit of N` ` ` `int` `MSB = log2(N);` ` ` `// Stores the number of steps` ` ` `// required to reduce N to 1` ` ` `int` `res = 0;` ` ` `// Iterates while N` ` ` `// is not equal 1` ` ` `while` `(N != 1) {` ` ` `// Increment res by 1` ` ` `res++;` ` ` `// If N is power of 2` ` ` `if` `(N & (N - 1) == 0) {` ` ` `// Divide N by 2` ` ` `N /= 2;` ` ` `}` ` ` `// Otherwise` ` ` `else` `{` ` ` `// Substract 2 ^ MSB` ` ` `// from N` ` ` `N -= (1 << MSB);` ` ` `}` ` ` `// Decrement MSB by 1` ` ` `MSB--;` ` ` `}` ` ` `// Returns res` ` ` `return` `res;` `}` `// Driver code` `int` `main()` `{` ` ` `// Input` ` ` `int` `N = 7;` ` ` `// Function call` ` ` `cout << reduceToOne(N) << endl;` `}` |

## Java

`/*package whatever //do not write package name here */` `import` `java.io.*;` `class` `GFG {` ` ` `public` `static` `int` `logarithm(` `int` `number, ` `int` `base)` ` ` `{` ` ` `int` `res = (` `int` `)(Math.log(number) / Math.log(base));` ` ` `return` `res;` ` ` `}` ` ` `public` `static` `int` `reduceToOne(` `int` `N)` ` ` `{` ` ` ` ` `// Stores the most` ` ` `// significant bit of N` ` ` `int` `MSB = logarithm(N, ` `2` `);` ` ` `// Stores the number of steps` ` ` `// required to reduce N to 1` ` ` `int` `res = ` `0` `;` ` ` `// Iterates while N` ` ` `// is not equal 1` ` ` `while` `(N != ` `1` `) {` ` ` `// Increment res by 1` ` ` `res++;` ` ` `// If N is power of 2` ` ` `if` `((N & (N - ` `1` `)) == ` `0` `) {` ` ` `// Divide N by 2` ` ` `N /= ` `2` `;` ` ` `}` ` ` `// Otherwise` ` ` `else` `{` ` ` `// Substract 2 ^ MSB` ` ` `// from N` ` ` `N -= (` `1` `<< MSB);` ` ` `}` ` ` `// Decrement MSB by 1` ` ` `MSB--;` ` ` `}` ` ` `// Returns res` ` ` `return` `res;` ` ` `}` ` ` `public` `static` `void` `main(String[] args)` ` ` `{` ` ` `int` `N = ` `7` `;` ` ` `int` `res = reduceToOne(N);` ` ` `System.out.println(res);` ` ` `}` `}` `// This code is contributed by lokeshpotta20.` |

## Python3

`# Python program for the above approach` `import` `math` `# Function to find steps needed to` `# reduce a given integer to 1` `def` `reduceToOne(N):` ` ` ` ` `# Stores the most` ` ` `# significant bit of N` ` ` `MSB ` `=` `math.floor(math.log2(N))` ` ` ` ` `# Stores the number of steps` ` ` `# required to reduce N to 1` ` ` `res ` `=` `0` ` ` ` ` `# Iterates while N` ` ` `# is not equal 1` ` ` `while` `(N !` `=` `1` `):` ` ` ` ` `# Increment res by 1` ` ` `res ` `+` `=` `1` ` ` ` ` `# If N is power of 2` ` ` `if` `(N & (N ` `-` `1` `) ` `=` `=` `0` `):` ` ` ` ` `# Divide N by 2` ` ` `N ` `/` `/` `=` `2` ` ` ` ` `# Otherwise` ` ` `else` `:` ` ` `# Substract 2 ^ MSB` ` ` `# from N` ` ` `N ` `-` `=` `(` `1` `<< MSB)` ` ` ` ` `# Decrement MSB by 1` ` ` `MSB` `-` `=` `1` ` ` ` ` `# Returns res` ` ` `return` `res` ` ` `# Driver code` `# Input` `N ` `=` `7` `# Function call` `print` `(reduceToOne(N))` `# This code is contibuted by shubham Singh` |

## C#

`// C# program for the above approach` `using` `System;` `using` `System.Collections.Generic;` `class` `GFG{` `// Function to find steps needed to` `// reduce a given integer to 1` `static` `int` `reduceToOne(` `int` `N)` `{` ` ` ` ` `// Stores the most` ` ` `// significant bit of N` ` ` `int` `MSB = (` `int` `)(Math.Log(N)/Math.Log(2));` ` ` `// Stores the number of steps` ` ` `// required to reduce N to 1` ` ` `int` `res = 0;` ` ` `// Iterates while N` ` ` `// is not equal 1` ` ` `while` `(N != 1) {` ` ` `// Increment res by 1` ` ` `res++;` ` ` `// If N is power of 2` ` ` `if` `((N & (N - 1)) == 0) {` ` ` `// Divide N by 2` ` ` `N /= 2;` ` ` `}` ` ` `// Otherwise` ` ` `else` `{` ` ` `// Substract 2 ^ MSB` ` ` `// from N` ` ` `N -= (1 << MSB);` ` ` `}` ` ` ` ` `// Decrement MSB by 1` ` ` `MSB--;` ` ` `}` ` ` ` ` `// Returns res` ` ` `return` `res;` `}` `// Driver code` `public` `static` `void` `Main()` `{` ` ` `// Input` ` ` `int` `N = 7;` ` ` ` ` `// Function call` ` ` `Console.Write(reduceToOne(N));` `}` `}` `// This code is contributed by bgangwar59.` |

## Javascript

`<script>` `// JavaScript program for the above approach` ` ` `function` `logarithm(number, base)` ` ` `{` ` ` `let res = (Math.log(number) / Math.log(base));` ` ` `return` `res;` ` ` `}` ` ` `function` `reduceToOne(N)` ` ` `{` ` ` ` ` `// Stores the most` ` ` `// significant bit of N` ` ` `let MSB = logarithm(N, 2);` ` ` `// Stores the number of steps` ` ` `// required to reduce N to 1` ` ` `let res = 0;` ` ` `// Iterates while N` ` ` `// is not equal 1` ` ` `while` `(N != 1) {` ` ` `// Increment res by 1` ` ` `res++;` ` ` `// If N is power of 2` ` ` `if` `((N & (N - 1)) == 0) {` ` ` `// Divide N by 2` ` ` `N /= 2;` ` ` `}` ` ` `// Otherwise` ` ` `else` `{` ` ` `// Substract 2 ^ MSB` ` ` `// from N` ` ` `N -= (1 << MSB);` ` ` `}` ` ` `// Decrement MSB by 1` ` ` `MSB--;` ` ` `}` ` ` `// Returns res` ` ` `return` `res;` ` ` `}` ` ` `// Driver Code` ` ` `let N = 7;` ` ` `let res = reduceToOne(N);` ` ` `document.write(res);` `</script>` |

**Output:**

2

**Time Complexity:** O(log(N))**Auxiliary Space:** O(1)

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the **Essential Maths for CP Course** at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more, please refer **Complete Interview Preparation Course****.**