
Here OR represents the bitwise operator | which returns 1 if any of the two bits is 1.
‘K’ is of the form 2^p -1 where ‘p’ is any positive integer.
‘ARR’ : {1, 2, 3} and ‘K’ : 2
Then the number of pairs with OR value greater than ‘K’ are : 2 i.e. {(2, 3), (1, 3)}
The first line of input contains an integer ‘T’ representing the number of test cases. Then the test cases follow :
The first line in each test case contains a single integer ‘N’ representing the size of the array ‘ARR’.
The second line in each test case contains ‘N’ integers separated by space representing elements of the array.
The third line in each test case contains a single integer ‘K’.
For each test case, print the total number of pairs with OR value greater than ‘K’ in a given array ‘ARR’.
You do not need to print anything, it has already been taken care of. Just implement the given function.
1 <= T <= 10
1 <= N <= 10^4
1<= ARR[i] <= 5*10^4
1 <= K <= 2^31 - 1
Time Limit : 1 sec
The simplest approach is to traverse the ‘ARR’ and generate all possible pairs and for each pair, check if the bitwise OR of the pair is greater than ‘K’ or not.
Here is the algorithm :
The approach is to find the number of elements greater than ‘K’. Elements greater than ‘K’ can form the pairs with every other element of the ‘ARR’. Because if a number is greater than ‘K’ then the OR value of it with any other number will also be greater than ‘K’.
Let’s take an example :
Let the array be ARR[] : {15, 2, 4}
Let ‘K’ be : 7
Bit representation of ‘K’ is : 0111
Bit representation of ‘ARR[0]’ i.e. 15 is : 1111
Bit representation of ‘ARR[1]’ i.e. 2 is : 0010
OR value of ‘ARR[0]’ and ‘ARR[1]’ : 15 | 2 = 15
While the numbers smaller than ‘K’ will not form pairs with each other. In the above example {4,2} will not form the pair (OR value of 4 and 2 is 6).
Here is the algorithm: