Maximum steps to transform 0 to X with bitwise AND

For an integer N, there are elements ranging from 0 to N-1. Certain elements can be transformed to other elements. Each transformation requires a certain effort which is equal to 1 unit, for each transformation. An element A can be transformed to element B, if and only if A != B and A & B = A (where & is the bitwise AND operator). We need find the maximum effort possible to obtain the element with value X,  from the element with value 0, by a series of transformations.

Examples :

Input : X = 2
Output : 1
The only way of obtaining 2 is to directly transform 0 to 2 (bitwise AND of 0 and 2 is 0) and hence requires one step.

Input : X = 3
Output : 2
3 can be obtained in two steps. First, transform 0 to 1 (bitwise AND of 0 and 1 is 0). Then, transform 1 to 3 (bitwise AND of 1 and 3 is 1).

The simple solution is to count the number of set bits in X.
Explanation:
First, consider a single step transformation from A to B. All the set bits (bits which are equal to 1) of A should be set in B, otherwise bitwise AND of A and B will not be equal to A. If there is any bit which is set in A but not in B, then the transformation is not possible. The unset bits of A can either be set or unset in B, it does not matter. We can then change A to B by setting all the unset bits in A in one single step. Consequently, if we had to transform 0 to X in least steps, the answer would have been one because bitwise AND of 0 with any number is 0.
But we have to compute the maximum steps. So in each step, we set each bit starting from the right and a set bit can not be cleared once it set.
Example:
Suppose we want to obtain 13 (1101 in binary) from 0. We start by setting the 1st bit from the right by transforming 0 to 1 (0001 in binary). We next set the 3rd bit from the right to form 5 (0101 in binary). The last step would be to set the 4th bit and obtain 13 (1101).

C++

 `// CPP code to find the maximum possible ` `// effort ` `#include ` `using` `namespace` `std; ` ` `  `// Function to get no of set bits in binary ` `// representation of positive integer n ` `unsigned ``int` `countSetBits(unsigned ``int` `n) ` `{ ` `    ``unsigned ``int` `count = 0; ` `    ``while` `(n) { ` `        ``count += n & 1; ` `        ``n >>= 1; ` `    ``} ` `    ``return` `count; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `i = 3; ` `    ``cout << countSetBits(i); ` `    ``return` `0; ` `} `

Java

 `// Java code to find the maximum ` `// possible effort ` ` `  `class` `GFG { ` `     `  `// Function to get no. of  ` `// set bits in binary ` `// representation of  ` `// positive integer n ` `static` `int` `countSetBits(``int` `n) ` `{ ` `    ``int` `count = ``0``; ` `    ``while` `(n != ``0``) ` `    ``{ ` `        ``count += n & ``1``; ` `        ``n >>= ``1``; ` `    ``} ` `    ``return` `count; ` `} ` ` `  `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` `    ``int` `i = ``3``; ` `    ``System.out.print(countSetBits(i)); ` `} ` `} ` ` `  `// This code is contributed by Smitha. `

Python3

 `# Python3 code to find the  ` `# maximum possible effort ` `   `  `# Function to get no of  ` `# set bits in binary  ` `# representation of positive ` `# integer n ` `def` `countSetBits(n) : ` `    ``count ``=` `0` `    ``while` `(n) : ` `        ``count ``+``=` `n & ``1` `        ``n >>``=` `1` `    ``return` `count ` `   `  `# Driver code ` `i ``=` `3` `print` `(countSetBits(i)) ` `   `  `# This code is contributed by  ` `# Manish Shaw(manishshaw1) `

C#

 `// C# code to find the maximum ` `// possible effort ` `using` `System; ` ` `  `class` `GFG { ` `     `  `// Function to get no. of  ` `// set bits in binary ` `// representation of  ` `// positive integer n ` `static` `int` `countSetBits(``int` `n) ` `{ ` `    ``int` `count = 0; ` `    ``while` `(n != 0) ` `    ``{ ` `        ``count += n & 1; ` `        ``n >>= 1; ` `    ``} ` `    ``return` `count; ` `} ` ` `  `// Driver code ` `public` `static` `void` `Main(String[] args) ` `{ ` `    ``int` `i = 3; ` `    ``Console.Write(countSetBits(i)); ` `} ` `} ` ` `  `// This code is contributed by Smitha. `

PHP

 `>= 1; ` `    ``} ` `    ``return` `\$count``; ` `} ` ` `  `// Driver code ` `\$i` `= 3; ` `echo` `(countSetBits(``\$i``)); ` ` `  `// This code is contributed by  ` `// Manish Shaw(manishshaw1) ` `?> `

Output :

`2`

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.