Maximum XOR With an Element From Array - Problem
Maximum XOR With an Element From Array
Imagine you're a data analyst tasked with finding the strongest possible signal by combining query values with array elements through XOR operations, but with a crucial constraint: you can only use array elements that don't exceed a given threshold.
You are given an array
For each query
If no elements in
Example: If
Imagine you're a data analyst tasked with finding the strongest possible signal by combining query values with array elements through XOR operations, but with a crucial constraint: you can only use array elements that don't exceed a given threshold.
You are given an array
nums consisting of non-negative integers, and a queries array where queries[i] = [xi, mi].For each query
i, your goal is to find the maximum bitwise XOR value of xi and any element from nums that is ≤ mi. In other words, find max(nums[j] XOR xi) for all valid j where nums[j] ≤ mi.If no elements in
nums satisfy the constraint nums[j] ≤ mi, return -1 for that query.Example: If
nums = [0,1,2,3,4] and you have query [3, 1], you can only use elements ≤ 1 (so 0 and 1), and max(3^0, 3^1) = max(3, 2) = 3. Input & Output
example_1.py — Basic Case
$
Input:
nums = [0,1,2,3,4], queries = [[3,1],[1,3],[5,6]]
›
Output:
[3,3,7]
💡 Note:
For query [3,1]: valid nums are {0,1}, max(3^0, 3^1) = max(3,2) = 3. For query [1,3]: valid nums are {0,1,2,3}, max XORs are {1,0,3,2}, so answer is 3. For query [5,6]: all nums are valid, max(5^0,5^1,5^2,5^3,5^4) = max(5,4,7,6,1) = 7.
example_2.py — No Valid Elements
$
Input:
nums = [5,2,4,6,6,3], queries = [[12,4],[8,1],[6,3]]
›
Output:
[15,-1,5]
💡 Note:
For query [12,4]: valid nums are {2,4,3}, max XORs are {14,8,15}, answer is 15. For query [8,1]: no nums ≤ 1, so return -1. For query [6,3]: valid nums are {2,3}, max XORs are {4,5}, answer is 5.
example_3.py — Single Element
$
Input:
nums = [0], queries = [[0,0],[1,0]]
›
Output:
[0,1]
💡 Note:
For query [0,0]: only 0 is valid, 0^0 = 0. For query [1,0]: only 0 is valid, 1^0 = 1.
Visualization
Tap to expand
Understanding the Visualization
1
Sort & Prepare
Sort nums and queries to enable incremental processing
2
Build Trie
Insert valid numbers into Trie as we process queries in order
3
Maximize XOR
For each query, traverse Trie choosing opposite bits when possible
4
Collect Results
Maintain original query order in final results
Key Takeaway
🎯 Key Insight: The Trie data structure is perfect for XOR maximization because it allows us to greedily select opposite bits at each level, and offline processing with sorting enables efficient constraint handling.
Time & Space Complexity
Time Complexity
O((N+Q)log(max_value))
Sorting takes O(NlogN + QlogQ), Trie operations take O(log(max_value)) per element/query
⚡ Linearithmic
Space Complexity
O(N*log(max_value))
Trie stores all numbers with up to log(max_value) bits each
✓ Linear Space
Constraints
- 1 ≤ nums.length, queries.length ≤ 105
- queries[i].length == 2
- 0 ≤ nums[j], xi, mi ≤ 109
- All values are non-negative integers
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code