
- 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
Count number of step required to reduce N to 1 by following certain rule 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
- Related Articles
- Count the number of operations required to reduce the given number in C++
- Count operations of the given type required to reduce N to 0 in C++
- Reduce a number to 1 by performing given operations in C++
- Count total number of digits from 1 to N in C++
- C++ program to count minimum number of operations needed to make number n to 1
- Find maximum operations to reduce N to 1 in Python
- Find maximum operations to reduce N to 1 in C++
- Count number of bits changed after adding 1 to given N in C++
- Minimum number of given moves required to make N divisible by 25 using C++.
- Program to count number of on lights flipped by n people in Python
- Queries to count the number of unordered co-prime pairs from 1 to N in C++
- Python program to count total set bits in all number from 1 to n.
- C++ program to count number of operations needed to reach n by paying coins
- Program to count number of swaps required to group all 1s together in Python
- Program to count number of minimum swaps required to make it palindrome in Python
