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.
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.
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.
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).
- 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 firstname.lastname@example.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.