# 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 <bits/stdc++.h> ` `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; ` `} ` |

*chevron_right*

*filter_none*

## 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. ` |

*chevron_right*

*filter_none*

## 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) ` |

*chevron_right*

*filter_none*

## 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. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP code to find the ` `// maximum possible effort ` ` ` `// Function to get no of ` `// set bits in binary ` `// representation of positive ` `// integer n ` `function` `countSetBits(` `$n` `) ` `{ ` ` ` `$count` `= 0; ` ` ` `while` `(` `$n` `) ` ` ` `{ ` ` ` `$count` `+= ` `$n` `& 1; ` ` ` `$n` `>>= 1; ` ` ` `} ` ` ` `return` `$count` `; ` `} ` ` ` `// Driver code ` `$i` `= 3; ` `echo` `(countSetBits(` `$i` `)); ` ` ` `// This code is contributed by ` `// Manish Shaw(manishshaw1) ` `?> ` |

*chevron_right*

*filter_none*

**Output :**

2

## Recommended Posts:

- Find subsequences with maximum Bitwise AND and Bitwise OR
- Maximum Bitwise AND pair from given range
- Maximum Bitwise AND value of subsequence of length K
- Maximum Bitwise OR pair from a range
- Maximum subset with bitwise OR equal to k
- Total pairs in an array such that the bitwise AND, bitwise OR and bitwise XOR of LSB is 1
- Count number of steps to cover a distance if steps can be taken in powers of 2
- Leftover element after performing alternate Bitwise OR and Bitwise XOR operations on adjacent pairs
- Minimize the number of steps required to reach the end of the array
- Sum of bitwise AND of all submatrices
- Sum of Bitwise-OR of all Submatrices
- Sum of bitwise OR of all possible subsets of given set
- Bitwise OR (or | ) of a range
- Sum of bitwise AND of all possible subsets of given set
- Sum of bitwise OR of all subarrays

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.