#1707

Maximum XOR With an Element From Array

expert · 1230 · lc hard +32 · premium · verified · 57.6% accepted · 1,405 likes · top 53%

Description

You are given an array nums of non-negative integers and queries[i] = [xi, mi]. For each query, find the maximum bitwise XOR of xi with any element of nums that does not exceed mi. If no such element exists, the answer for that query is -1.

Return an integer array answer where answer[i] is the result of the ith query.

Example 1:

Input: nums = [0,1,2,3,4], queries = [[3,1],[1,3],[5,6]]
Output: [3,3,7]
Explanation:
1) 0 and 1 are the only two integers not greater than 1. 0 XOR 3 = 3 and 1 XOR 3 = 2. The larger of the two is 3.
2) 1 XOR 2 = 3.
3) 5 XOR 2 = 7.

Example 2:

Input: nums = [5,2,4,6,6,3], queries = [[12,4],[8,1],[6,3]]
Output: [15,-1,5]

Code

1
2
3