#250HardTries· Binary Trie with Offline Queries
Max XOR Under a Limit
Input format
`nums`: an array of non-negative integers; `queries`: an array of pairs `[x, m]`.
Output format
An array with one integer per query: the largest XOR value, or -1 when no code is at most m.
Examples
Input: nums = [0, 1, 2, 3, 4], queries = [[3, 1], [1, 3], [5, 6]]
Output: [3, 3, 7]
For [3,1] the allowed codes are 0 and 1: 3^0=3 and 3^1=2, the best is 3. For [1,3] the allowed codes are 0..3 and 1^2=3 is the best. For [5,6] all codes are allowed and 5^2=7 is the best.
Input: nums = [5, 2, 4, 6, 6, 3], queries = [[12, 4], [8, 1], [6, 3]]
Output: [15, -1, 5]
For [12,4] the allowed codes are 2, 3 and 4: 12^3=15 is the best. For [8,1] no code is at most 1, so -1. For [6,3] the allowed codes are 2 and 3: 6^2=4 and 6^3=5, so 5.
Constraints
- 1 <= nums.length <= 10^5
- 1 <= queries.length <= 10^5
- 0 <= nums[i], x < 2^30
- 0 <= m < 2^30
Study first
- #trie
- #bit-manipulation
- #offline-queries
- #sorting
- #xor