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 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
XOR Maximization with Trie StrategyInput Data:nums = [0,1,2,3,4] queries = [[3,1], [1,3], [5,6]]Trie Structure (Binary Representation):ROOTbit=0bit=101Numbers: 0,2,4Numbers: 1,3XOR Maximization Process:Query [3,1]: Find max XOR with nums ≤ 1• Available: {0, 1}• Binary of 3: 011, want opposite bits• Try 3^0=3, 3^1=2 → max = 3Query [1,3]: Find max XOR with nums ≤ 3• Available: {0,1,2,3} → max XOR = 3Algorithm Steps:1. Sort: Sort nums array and queries by constraint mi2. Process: For each query in order, add valid nums to Trie3. Search: Use Trie to find maximum XOR by choosing opposite bits greedily4. Result: Return answers in original query orderTime: O((N+Q) × log(max_val))Space: O(N × log(max_val))Trie depth = log(max_value)🎯 Key Insight:XOR is maximized when bits differ most.Trie enables greedy bit-by-bit optimization!
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

n
2n
Linearithmic
Space Complexity
O(N*log(max_value))

Trie stores all numbers with up to log(max_value) bits each

n
2n
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
Asked in
Google 45 Amazon 38 Meta 32 Microsoft 28
47.5K Views
High Frequency
~25 min Avg. Time
1.3K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen