


1. If all integers are greater than ‘Ai’ in array/list ‘ARR’ then the answer to this ith query will be -1.
The first line contains a single integer ‘T’ representing the number of test cases.
The first line of each test case will contain two space-separated integers ‘N’ and ‘M’ representing the size of array/list ‘ARR’ and number of queries respectively.
The second line of each test case will contain ‘N’ space-separated integers representing array/list ‘ARR’.
The next ‘M’ line of each test case contains the description of ‘QUERIES’. The ‘i-th’ line of these ‘M’ lines consists of two space-separated integers ‘Xi’, ‘Ai’ as described in the problem statement.
For each test case, print ‘M’ space-separated integer where the ‘i-th’ integer is the answer of the ‘i-th’ query.
Output for every test case will be printed in a separate line.
You don’t need to print anything; It has already been taken care of.
1 <= T <= 50
1 <= N, M <= 10000
0 <= ARR[i], Xi, Ai <= 10^9
Where ‘T’ is the number of test cases, 'N' is the size of ‘ARR’, ‘M’ is the number of queries, ‘ARR[i]’ is an element of array/list ‘ARR’ and ‘Xi’, ‘Ai’ are the integers in ‘QUERIES[i]’.
Time limit: 1 sec
The basic idea is to iterate over the array ‘ARR’ for each query. For the ith query i.e ‘QUERIES[i]’, we iterate over ‘ARR’ and find the xor of ‘Xi’ with every integer in ‘ARR’ smaller or equal to ‘Ai’, If every integer in ‘ARR’ is greater than ‘Ai’, then the answer of this query is -1 otherwise, it is maximum xor value.
The steps are as follows:
The basic idea is to sort the given array ‘ARR’ in non-decreasing order. Then for each query maintains two pointers ‘LEFT’, ‘RIGHT’, such that the integer that gives maximum xor with Xi is an index in the range [LEFT, RIGHT).
Now for each query ‘i’, We enumerate over bits of ‘Xi’ from MSB (Most significant bit) to LSB (Least significant bit), For each position of bit, if the bit of ‘Xi‘ in that position is 0, we try to find an integer with bit 1 in that position in the range [LEFT, RIGHT) of ‘ARR’. We can find it using binary search in O(logN) times and update this range accordingly. Similarly, if the bit of ‘Xi’ in that position is 1, we try to find an integer with bit 0 in that position in the range [LEFT, RIGHT) of ‘ARR’ and update this range accordingly.
The steps are as follows:
The basic idea is to convert each integer in ‘ARR’ in the binary format with exactly 31 bits and construct a trie. Trie here is basically a rooted binary tree where each edge is either labelled by 0 or 1. We can query maximum bitwise XOR in O(31) times using trie.
We process ‘QUERIES’ in non-decreasing order of ‘Ai’, and construct the trie on the fly. Before processing the ‘i-th’ query, we make sure only those integers of ‘ARR’ that do not exceed ‘Ai’ are added to the trie.
The steps are as follows: