
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
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
Count Steps to Reduce N to 1 in C++
We are given a number N. The goal is to count the number of steps required to reduce the number to 1 by following rules −
If the number is power of 2, reduce it to its half.
Else reduce it to the N-(nearest power of 2 which is less than N).
For step 1, we will check if N is power of 2, by checking if ceil(log2(N)), floor(log2(N)) return the same result. If yes then N=N/3, increment count of operation.
If the result of step 1 is false then we will perform step 2 and subtract the nearest power of 2 less than N from N. Nearest power of 2 less than N will be calculated as −
x=floor(log2(N)) → when N is not power of 2, log2(N) gives floating point value, floor reduces it to the nearest integer less than N.
Now N=N-pow(2,x) → pow(2,x) will give the nearest power of 2 less than N. Reduce N.
Let’s understand with examples.
Input − N=20
Output-: Count of steps required − 3
Explanation − N=20
20 is not power of 2. Step 2. Reduce nearest power of 2 less than N from N. N=20- 16=4. Count=1. 4 is power of 2. Step 1. Reduce N to its half. N=4/2=2. Count=2. 2 is power of 2. Step 1. Reduce N to its half. N=2/2=1. Count=3. N is 1 total step count=3.
Input − N=32
Output Count of steps required − 5
Explanation − N=32
32 is power of 2. Step 1. Reduce N to its half. N=32/2=16. Count=1. 16 is power of 2. Step 1. Reduce N to its half. N=16/2=8. Count=2. 8 is power of 2. Step 1. Reduce N to its half. N=8/2=4. Count=3. 4 is power of 2. Step 1. Reduce N to its half. N=4/2=2. Count=4. 2 is power of 2. Step 1. Reduce N to its half. N=2/2=1. Count=5. N is 1 total step count=5.
Approach used in the below program is as follows
We take an integer N for storing an integer value.
Function stepCount(int n) takes N and returns the count of steps required to reduce it to 1.
Take the initial count of steps as 0.
Now while(n!=1) perform both steps 1, and 2 according to the value of n.
If n is power of 2 ( ceil(log2(n))==floor(log2(n)) will be true ), reduce n to half. Increment count.
If not power of 2 then reduce n by pow(2,x) where x is floor(log2(n)). Increment count.
When the loop will be over then count will have the number of operations performed.
Return count as desired result.
Example
#include <iostream> #include <math.h> using namespace std; // Function to return number of steps for reduction int stepCount(int n){ int count=0; while(n!=1){ if(ceil(log2(n))==floor(log2(n))) //if n is power of 2 then this is true{ n=n/2; //reduce n to half count++; } else { int x=floor(log2(n)); //floor value n=n-(pow(2,x)); //2^x is nearest power of 2 which is less than n count++; } } return count; } int main(){ int N = 96; cout <<"Count of steps required to reduce N to 1:"<<stepCount(N); return 0; }
Output
If we run the above code it will generate the following output −
Count of steps required to reduce N to 1:6