Maximum XOR With an Element From Array
Implement maximumXorQueries
You're given an array
nums and a batch of queries, each one a pair [x, m]. For every query, find the largest XOR that x can produce against any value in nums that doesn't exceed the limit m — and if nothing in nums qualifies, the answer for that query is -1.
Answering each query by rescanning the array works, but every query's eligible set is really just a growing subset of nums as the limit increases. Sorting the queries by their limit and sorting nums once turns this into a single sweep: insert values into the answer structure only as they become eligible, and each query gets answered against everything inserted so far — no value ever needs to be considered by two different rescans.
Example 1:
Input: nums = [0,1,2,3,4], queries = [[3,1],[1,3],[5,6]]
Output: [3,3,7]
Example 2:
Input: nums = [5,2,4,6,6,3], queries = [[12,4],[8,1],[6,3]]
Output: [15,-1,5]
Example 3:
Input: nums = [1], queries = [[1,0]]
Output: [-1]
+ 8 hidden test cases run on Submit.
Constraints:
- ●
1 ≤ nums.length ≤ 10⁴ - ●
1 ≤ queries.length ≤ 10⁴ - ●
0 ≤ nums[i] ≤ 10⁹ - ●
0 ≤ x ≤ 10⁹ and 0 ≤ m ≤ 10⁹ for every query [x, m] - ●
If no value in nums is ≤ m, the answer for that query is -1
nums =
[0, 1, 2, 3, 4]
queries =
[[3,1], [1,3], [5,6]]