- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- 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

In this problem, we are given an integer X. Our task is to find the total number of steps that are taken to transform from 0 to X.

**Valid transformation** − One step is counted when one transformation takes place from A to B. The condition for the transform to take place is A != B and A & B = A (& is bitwise AND). So, 1 step is transforming from A to B and we have to create a program that will count the maximum number of steps to transform 0 to X.

Let’s take an example to understand the problem,

**Input** − X = 7

**Output** − 3

**Explanation** −

We have to transform from 0 to 7.

Steps taken will be Step1: 0(00) to 1(01) , 0!= 1 and 0&1 = 0, transform 00=>01 Step2: 1(001) to 3(011) , 1!= 3 and 1&3 = 1, transform 001=>011 Step3: 3(0011) to 7(0111) , 3!= 7 and 3&7 = 3, tranform 0011=>0111.

To solve this problem, we will be counting the number of set bits in X which will give the maximum transformation from 0 to X.

As we need the maximum transformation, we have to go step by step for each set bit(the bit with value 1). Bit after bit will transformation will give the maximum steps to transform from 0 to X.

In the transformation from A to B, all the set bits of A should be set in B, but the reverse is not necessary. So, the minimum transformation could be 1 as there are no set bits in 0 which makes the smallest conversion direct.

As we can see in the example we have taken, the binary conversion on numbers and their bitwise ANDs.

**Program to show the implementation of our solution −**

//Program to find Maximum steps to transform 0 to X with bitwise AND in C++

#include <bits/stdc++.h> using namespace std; int maxtransformation(int x){ int steps = 0; // counting number of bits while (x) { steps += x & 1; x >>= 1; } return steps; } int main(){ int x = 7; cout<<"The maximum number of steps to transform 0 to "<<x<<" with bitwise AND are "<<maxtransformation(x); return 0; }

The maximum number of steps to transform 0 to 7 with bitwise AND are 3

- Related Questions & Answers
- Find subsequences with maximum Bitwise AND and Bitwise OR in Python
- Check if it is possible to move from (0, 0) to (x, y) in N steps in Python
- Find maximum value of x such that n! % (k^x) = 0 in C++
- Maximum subset with bitwise OR equal to k in C++
- X-axis 3D transform with CSS3
- Maximum Bitwise AND pair from given range in C++
- Triples with Bitwise AND Equal To Zero in C++
- Transform the element along with x-axis and y-axis with CSS
- Transform the element using x-axis with CSS3
- Transform the element along with x-axis using CSS
- Maximum bitwise AND value of a pair in an array in C++
- Rotate transform the element by using x-axis with CSS3
- Find maximum among x^(y^2) or y^(x^2) where x and y are given in C++
- Transform the element by using x-axis, y-axis, and z-axis with CSS3
- Transform to Chessboard in C++

Advertisements