Given three positive integers X, Y and Z,the task is to count minimum numbers of bits required to be flipped in X and Y such that X OR Y (X | Y) is equal to Z.
Input : X = 5 Y = 8 Z = 6 Output : 3 Explanation : Before After Flipping Flipping X :: 0101 0100 Y :: 1000 0010 ------ ----- Z :: 0110 0110 So we need to flip 3 bits in order to make (X | Y) = Z . Input : X = 1 Y = 2 Z = 3 Output : 0
In order to solve this problem, we need to observe that the need to flip a bit X or Y arises only for the following conditions:
- If the current bit in Z is set and the corresponding bit in both X and Y is not set, we need to set a bit in either X or in Y.
- If the current bit in Z is unset, we need to unset the corresponding bit in A and B, whichever is set. Unset both if both are set.
Thus, we need to traverse through the bits of X, Y and Z and follow the above two steps to get the desired result.
Below is the implementation of the above approach:
- Count number of bits to be flipped to convert A to B | Set-2
- Count number of bits to be flipped to convert A to B
- Count of total bits toggled/flipped in binary representation of 0 to N
- Minimum integer with at most K bits set such that their bitwise AND with N is maximum
- Largest set with bitwise OR equal to n
- Numbers whose bitwise OR and sum with N are equal
- Minimize operations required to make each element of Array equal to it's index value
- Minimize increment/decrement of Array elements to make each modulo K equal
- Maximum subset with bitwise OR equal to k
- Largest possible value of M not exceeding N having equal Bitwise OR and XOR between them
- Find N distinct numbers whose bitwise Or is equal to K
- Count of pairs having bit size at most X and Bitwise OR equal to X
- Count of pairs from Array with sum equal to twice their bitwise AND
- Minimum Bitwise OR operations to make any two array elements equal
- XOR of all elements of array with set bits equal to K
- Sorting array elements with set bits equal to K
- Count minimum bits to flip such that XOR of A and B equal to C
- Find a number X such that (X XOR A) is minimum and the count of set bits in X and B are equal
- Count pairs in an array such that both elements has equal set bits
- Total pairs in an array such that the bitwise AND, bitwise OR and bitwise XOR of LSB is 1
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.