oneby7

Tries Tries over bits

430. Maximum XOR With an Element From Array

Hard

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]]

Output: [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]]

Output: [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]]

Output: [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.

nums = [4, 1, 7], queries = [[3, 4], [2, 0]]

Constraints

  • 1 ≤ nums.length ≤ 105
  • 0 ≤ nums[j] ≤ 109
  • 1 ≤ queries.length ≤ 105
  • queries[i].length = 2
  • 0 ≤ x, m ≤ 109

Clarifying questions

What you would ask an interviewer before writing any code.

Hints

Interview follow-ups

Runs in your browser
Code
Runs in your browser for now · some things may not work as expectedLn 1, Col 1

Article

Send feedback

Thank you for helping. We read every report and fix what is wrong.

How do you feel about oneby7?

Sign in to oneby7

Keep your progress, your streak and your code, on any device.

New here? This creates your account too.