Maximum XOR With an Element From Array
Pattern: Maximum XOR
Each query asks for the best XOR with numbers no greater than a limit. Sort the numbers, answer the queries in order of their limits, and before each one insert the numbers up to its limit into a binary trie.
The problem
For each query [x, m], return the largest x XOR nums[j] over elements with nums[j] <= m, or -1 if no element is that small.
Think first
- Why is the limit awkward for one trie of all numbers?
- What if the queries came with increasing limits?
- When is the answer -1?
Hints
Brute force, and why it fails
For each query scan every number: O(q × n).
The key insight
Sorting queries by limit means the trie always holds exactly the allowed numbers.
Dry run
nums [0, 1, 2, 3, 4]. Query (3, 1): only 0 and 1 are allowed: 3 XOR 0 = 3.
Step 1 of 2
The plan
sort nums; sort query indexes by m
for each query: insert nums while nums[k] <= m
if trie empty: -1 else best XOR with x
Complexity
- Time
O((n + q) × 31 + sorting)- Space
O(31 × n)
Each number is inserted once.
Edge cases
- All numbers above the limit: -1.
- Equal limits.
- x = 0.
I've built my logic, show the code
Java
import java.util.*;
class Solution {
public int[] maximizeXor(int[] nums, int[][] queries) {
int[] a = nums.clone();
Arrays.sort(a);
Integer[] order = new Integer[queries.length];
for (int i = 0; i < order.length; i++) order[i] = i;
Arrays.sort(order, (p, q) -> Integer.compare(queries[p][1], queries[q][1])); // answer queries by limit, smallest first
int[][] next = new int[a.length * 31 + 1][2];
int nodes = 1, added = 0;
int[] out = new int[queries.length];
for (int qi : order) {
int x = queries[qi][0], limit = queries[qi][1];
while (added < a.length && a[added] <= limit) { // the trie holds exactly the numbers <= limit
int n = 0;
for (int b = 30; b >= 0; b--) {
int bit = a[added] >> b & 1;
if (next[n][bit] == 0) next[n][bit] = nodes++;
n = next[n][bit];
}
added++;
}
if (added == 0) { out[qi] = -1; continue; } // no number is small enough
int m = 0, got = 0;
for (int b = 30; b >= 0; b--) {
int want = (x >> b & 1) ^ 1;
if (next[m][want] != 0) { got |= 1 << b; m = next[m][want]; }
else m = next[m][want ^ 1];
}
out[qi] = got;
}
return out;
}
}C++
class Solution {
public:
vector<int> maximizeXor(vector<int>& nums, vector<vector<int>>& queries) {
vector<int> a(nums);
sort(a.begin(), a.end());
vector<int> order(queries.size());
for (size_t i = 0; i < order.size(); i++) order[i] = static_cast<int>(i);
sort(order.begin(), order.end(), [&](int p, int q) { return queries[p][1] < queries[q][1]; }); // by limit
vector<vector<int>> next(1, vector<int>(2, 0));
size_t added = 0;
vector<int> out(queries.size());
for (int qi : order) {
int x = queries[qi][0], limit = queries[qi][1];
while (added < a.size() && a[added] <= limit) { // the trie holds exactly the numbers <= limit
int n = 0;
for (int b = 30; b >= 0; b--) {
int bit = a[added] >> b & 1;
if (next[n][bit] == 0) {
next[n][bit] = static_cast<int>(next.size());
next.push_back(vector<int>(2, 0));
}
n = next[n][bit];
}
added++;
}
if (added == 0) { out[qi] = -1; continue; } // no number is small enough
int m = 0, got = 0;
for (int b = 30; b >= 0; b--) {
int want = (x >> b & 1) ^ 1;
if (next[m][want] != 0) { got |= 1 << b; m = next[m][want]; }
else m = next[m][want ^ 1];
}
out[qi] = got;
}
return out;
}
};Python
class Solution:
def maximizeXor(self, nums: list[int], queries: list[list[int]]) -> list[int]:
a = sorted(nums)
order = sorted(range(len(queries)), key=lambda i: queries[i][1]) # answer queries by limit, smallest first
root, added, out = {}, 0, [0] * len(queries)
for qi in order:
x, limit = queries[qi]
while added < len(a) and a[added] <= limit: # the trie holds exactly the numbers <= limit
node = root
for b in range(30, -1, -1):
node = node.setdefault(a[added] >> b & 1, {})
added += 1
if added == 0:
out[qi] = -1 # no number is small enough
continue
node, got = root, 0
for b in range(30, -1, -1):
want = (x >> b & 1) ^ 1
if want in node:
got |= 1 << b
node = node[want]
else:
node = node[want ^ 1]
out[qi] = got
return outCommon mistakes
- Answering in sorted order but returning them unsorted.
- Including numbers above the limit.