Tries Tries over bits
430. Maximum XOR With an Element From Array
You are given an array of non-negative integers nums and a list of queries, where queries[i] = [x, m].
For each query, look only at the numbers in nums that are at most m. Among them, pick the one whose
bitwise XOR with x is largest. The answer to the query is that largest value of x XOR nums[j]. If no
number in nums is at most m, the answer is -1.
Return the answers in the same order as queries.
Example 1
Input: nums = [6, 2, 9, 4, 13]
queries = [[5, 8], [10, 3], [1, 1], [7, 13]]
[7,8,-1,14]Explanation: For [5,8], the numbers 6, 2 and 4 are at most 8, and 5 XOR 2 = 7 is the largest. Only 2 is at most 3,
and 10 XOR 2 = 8. No number is at most 1. In the last query every number is allowed, and
7 XOR 9 = 14.
Example 2
Input: nums = [0, 3, 3], queries = [[0, 0], [3, 3], [0, 2]]
[0,3,0]Explanation: 0 is allowed in every query, so no answer is -1. In the second query 3 is allowed too, since it equals
m, but 3 XOR 0 = 3 beats 3 XOR 3 = 0.
Example 3
Input: nums = [1000000000]
queries = [[0, 1000000000],
[536870911, 999999999]]
[1000000000,-1]Explanation: The one number is allowed in the first query. In the second it is just above m.
Check your understanding
Work out the answer for this input before you write any code.
Constraints
1 ≤ nums.length ≤ 1050 ≤ nums[j] ≤ 1091 ≤ queries.length ≤ 105queries[i].length = 20 ≤ x, m ≤ 109
Clarifying questions
What you would ask an interviewer before writing any code.