Input: X = 2, Y = 5
The two possible pairs are (5, 7) and (7, 5).
Pair 1: (5, 7)
Bitwise AND = 5 & 7 = 2
Bitwise XOR = 5 ^ 7 = 5
Pair 2: (7, 5)
Bitwise AND = 7 & 5 = 2
Bitwise XOR = 7 ^ 5 = 5
Input: X = 7, Y = 5
Naive Approach: The simplest approach to solve the problem is to choose the maximum among X and Y and set all its bits and then check all possible pairs from 0 to that maximum number, say M. If for any pair of A and B, A & B and A⊕B becomes equal to X and Y respectively, then increment the count. Print the final value of count after checking for all possible pairs.
Time Complexity: O(M2)
Auxiliary Space: O(1)
Efficient Approach: The idea is to generate all possible combinations of bits at each position. There are 4 possibilities for the ith bit in X and Y which are as follows:
- If Xi = 0 and Yi = 1, then Ai = 1 and Bi = 1.
- If Xi = 1 and Yi = 1, then no possible bit assignment exist.
- If Xi = 0 and Yi = 0 then Ai = 0 and Bi = 0.
- If Xi = 1 and Yi = 0 then Ai = 0 and Bi = 1 or Ai = 1 and Bi = 0, where Ai, Bi, Xi, and Yi represent ith bit in each of them.
Follow the steps below to solve the problem:
- Initialize the counter as 1.
- For the ith bit, if Xi and Yi are equal to 1, then print 0.
- If at ith bit, Xi is 1 and Yi is 0 then multiply the counter by 2 as there are 2 options.
- After that, divide X and Y each time by 2.
- Repeat the above steps for every ith bit until both of them become 0 and print the value of the counter.
Below is the implementation of the above approach:
Time Complexity: O(log M)
Auxiliary Space: O(1)
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.
- Total pairs in an array such that the bitwise AND, bitwise OR and bitwise XOR of LSB is 1
- Generate an Array in which count of even and odd sum sub-arrays are E and O respectively
- Maximize array sum by replacing equal adjacent pairs by their sum and X respectively
- Count pairs with bitwise XOR exceeding bitwise AND from a given array
- Count ways to make Bitwise XOR of odd and even indexed elements equal by removing an array element
- Count pairs having Bitwise XOR less than K from given array
- Count pairs having bitwise XOR greater than K from a given array
- 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
- Calculate Bitwise OR of two integers from their given Bitwise AND and Bitwise XOR values
- Count pairs with equal Bitwise AND and Bitwise OR value
- Subsequence pair from given Array having all unique and all same elements respectively
- Leftover element after performing alternate Bitwise OR and Bitwise XOR operations on adjacent pairs
- Minimize array sum by replacing greater and smaller elements of pairs by half and double of their values respectively atmost K times
- Count of pairs having bit size at most X and Bitwise OR equal to X
- Highest and Smallest power of K less than and greater than equal to N respectively
- Largest possible value of M not exceeding N having equal Bitwise OR and XOR between them
- Non-negative pairs with sum of Bitwise OR and Bitwise AND equal to N
- Count ways to split a Binary String into three substrings having equal count of zeros
- Count of rectangles possible from N and M straight lines parallel to X and Y axis respectively
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.