Given a number X, calculate number of possible pairs (a, b) such that bitwise or of a and b is equal to X and number of bits in both a and b is less than equal to number of bits in X.
Input: X = 6
The possible pairs of (a, b) are (4, 6), (6, 4), (6, 6), (6, 2), (4, 2), (6, 0), (2, 6), (2, 4), (0, 6).
Input: X = 21
In total there are 27 pairs possible.
Approach: To solve the problem mentioned above follow the steps given below:
- Iterate through every bit of given number X.
- If the bit is 1 then from the truth table of Bitwise OR we know that there are 3 combinations possible for that given bit in number a and b that is (0, 1), (1, 0), (1, 1) that is 3 possible ways.
- If the bit is 0 then from the truth table of Bitwise OR we know that there is only 1 combination possible for that given bit in number a and b that is (0, 0).
- So our answer will be answer will be 3 ^ (number of on bits in X).
Below is the implementation of above approach:
Time complexity: O(log(X))
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Count ways to generate pairs having Bitwise XOR and Bitwise AND equal to X and Y respectively
- Total pairs in an array such that the bitwise AND, bitwise OR and bitwise XOR of LSB is 1
- Count pairs with equal Bitwise AND and Bitwise OR value
- Non-negative pairs with sum of Bitwise OR and Bitwise AND equal to N
- Count pairs with bitwise XOR exceeding bitwise AND from a given array
- Maximize sum of squares of array elements possible by replacing pairs with their Bitwise AND and Bitwise OR
- Count of binary strings of length N having equal count of 0's and 1's and count of 1's ≥ count of 0's in each prefix substring
- Leftover element after performing alternate Bitwise OR and Bitwise XOR operations on adjacent pairs
- Minimum possible Bitwise OR of all Bitwise AND of pairs generated from two given arrays
- Count of binary strings of length N with even set bit count and at most K consecutive 1s
- Count pairs having Bitwise XOR less than K from given array
- Count pairs having bitwise XOR greater than K from a given array
- Count of pairs from Array with sum equal to twice their bitwise AND
- Minimum possible value T such that at most D Partitions of the Array having at most sum T is possible
- Count even length subarrays having bitwise XOR equal to 0
- Count nodes having Bitwise XOR of all edges in their path from the root equal to K
- Count pairs from given array with Bitwise OR equal to K
- Largest possible value of M not exceeding N having equal Bitwise OR and XOR between them
- Count of all possible pairs having sum of LCM and GCD equal to N
- Count pairs from an array having product of their sum and difference equal to 0
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.