You are given an array/list ‘ARR’ consisting of ‘N’ non-negative integers. You are also given a list ‘QUERIES’ consisting of ‘M’ queries, where the ‘i-th’ query is a list/array of two non-negative integers ‘Xi’, ‘Ai’, i.e ‘QUERIES[i]’ = [‘Xi’, ‘Ai’].
The answer to the ith query, i.e ‘QUERIES[i]’ is the maximum bitwise xor value of ‘Xi’ with any integer less than or equal to ‘Ai’ in ‘ARR’.
You should return an array/list consisting of ‘N’ integers where the ‘i-th’ integer is the answer of ‘QUERIES[i]’.
Note:
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.
Output Format:
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.
Note:
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
2
5 2
0 1 2 3 4
1 3
5 6
1 1
1
1 0
3 7
-1
In the first test case, the answer of query [1, 3] is 3 because 1^2 = 3 and 2 <= 3, and the answer of query [5, 6] is 7 because 5 ^ 2 = 7 and 2 <= 6.
In the second test case, no element is less than or equal to 0 in the given array ‘ARR’.
2
6 3
6 6 3 5 2 4
6 3
8 1
12 4
5 2
0 0 0 0 0
1 0
1 1
5 -1 15
1 1
For each query try out all integers in the given array.
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:
O(N*M), where 'N' is the size of ‘ARR’, and ‘M’ is the number of queries.
There are ‘M’ queries, and for each query, we iterate over an array ‘ARR’ of size ‘N’ and find xor with integers in it. Thus, the overall time complexity will be O(N*M). :
O(1)
We are using constant extra space here.