Maximum XOR With an Element From Array

You are given an array nums consisting of non-negative integers. You are also given a queries array, where queries[i] = [xi, mi].

The answer to the i^th query is the maximum bitwise XOR value of xi and any element of nums that does not exceed mi. In other words, the answer is max(nums[j] XOR xi) for all j such that nums[j] <= mi. If all elements in nums are larger than mi, then the answer is -1.

Return an integer array answer where answer.length == queries.length and answer[i] is the answer to the i^th query.

Example 1
Inputnums = [0,1,2,3,4], queries = [[3,1],[1,3],[5,6]]
Output[3,3,7]
For the first query, only 0 and 1 are not greater than 1, giving maximum XOR 3; for the second and third queries, the maximum XOR values are 3 and 7 respectively.
Example 2
Inputnums = [5,2,4,6,6,3], queries = [[12,4],[8,1],[6,3]]
Output[15,-1,5]
The best valid XOR values for the three queries are 15, -1 because no number is at most 1, and 5 respectively.

Constraints

  • 1 <= nums.length, queries.length <= 10^5
  • queries[i].length == 2
  • 0 <= nums[j], xi, mi <= 10^9

Asked at 1 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate