Maximum XOR With an Element From Array - Problem

You are given an array nums consisting of non-negative integers. You are also given a queries array, where queries[i] = [xi, mi].

The answer to the ith query is the maximum bitwise XOR value of xi and any element of nums that does not exceed mi. In other words, the answer is max(nums[j] XOR xi) for all j such that nums[j] <= mi. If all elements in nums are larger than mi, then the answer is -1.

Return an integer array answer where answer.length == queries.length and answer[i] is the answer to the ith query.

Input & Output

Example 1 — Basic Queries
$ Input: nums = [0,1,2,3,4], queries = [[3,1],[1,3],[5,6]]
Output: [3,3,7]
💡 Note: Query [3,1]: Valid elements ≤1 are {0,1}. Max XOR: max(0⊕3, 1⊕3) = max(3,2) = 3. Query [1,3]: Valid elements ≤3 are {0,1,2,3}. Max XOR: max(0⊕1, 1⊕1, 2⊕1, 3⊕1) = max(1,0,3,2) = 3. Query [5,6]: All elements ≤6, Max XOR: max(0⊕5, 1⊕5, 2⊕5, 3⊕5, 4⊕5) = max(5,4,7,6,1) = 7.
Example 2 — No Valid Elements
$ Input: nums = [5,2,4,6,6,3], queries = [[12,4],[8,1],[6,3]]
Output: [15,-1,5]
💡 Note: Query [12,4]: Valid elements ≤4 are {2,4,3}. Max XOR: max(2⊕12, 4⊕12, 3⊕12) = max(14,8,15) = 15. Query [8,1]: No elements ≤1, return -1. Query [6,3]: Valid elements ≤3 are {2,3}. Max XOR: max(2⊕6, 3⊕6) = max(4,5) = 5.
Example 3 — Edge Case
$ Input: nums = [0], queries = [[0,0]]
Output: [0]
💡 Note: Single element case: 0 ≤ 0, so 0 XOR 0 = 0.

Constraints

  • 1 ≤ nums.length, queries.length ≤ 105
  • queries[i].length == 2
  • 0 ≤ nums[j], xi, mi ≤ 109

Visualization

Tap to expand
Maximum XOR With an Element From Array INPUT nums array: 0 1 2 3 4 queries: x=3, m=1 Query 0 x=1, m=3 Query 1 x=5, m=6 Query 2 Binary: 0: 000 1: 001 2: 010 3: 011 4: 100 5: 101 Trie Structure: R 0 1 0 1 ALGORITHM STEPS 1 Sort queries by m Process offline: smallest m first 2 Sort nums array Insert incrementally into Trie 3 Build Trie incrementally Add nums[j] while nums[j] <= mi 4 Query max XOR Greedily pick opposite bits Processing Example: Q0: m=1, x=3 (011) Trie has: 0,1 --> max XOR=3 Q1: m=3, x=1 (001) Trie has: 0,1,2,3 --> max XOR=3 Q2: m=6, x=5 (101) Trie has: 0-4 --> 5 XOR 2=7 FINAL RESULT Output Array: 3 3 7 Verification: Q0: 3 XOR 0 = 3 [OK] (0,1 valid, best=0) Q1: 1 XOR 2 = 3 [OK] (0,1,2,3 valid, best=2) Q2: 5 XOR 2 = 7 [OK] (all valid, best=2) XOR Calculation: 5: 101 2: 010 7: 111 XOR Key Insight: Offline processing sorts queries by 'm' to incrementally build the Trie. For each query, we add numbers <= m to the Trie before querying. To maximize XOR, greedily choose the opposite bit at each level (if 1 in x, prefer 0 in Trie and vice versa). TutorialsPoint - Maximum XOR With an Element From Array | Trie with Offline Processing
Asked in
Google 15 Microsoft 12 Amazon 8 Facebook 6
28.0K Views
Medium Frequency
~35 min Avg. Time
892 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