|
| 1 | +--- |
| 2 | +id: maximum-xor-with-an-element-from-array |
| 3 | +title: Maximum XOR With an Element From Array |
| 4 | +sidebar_label: 1707 - Maximum XOR With an Element From Array |
| 5 | +tags: [Trie, Bit Manipulation, Sorting, C++] |
| 6 | +description: Solve the problem of finding the maximum bitwise XOR of an element with given elements that do not exceed a specified value using trie and bit manipulation. |
| 7 | +--- |
| 8 | + |
| 9 | +## Problem Statement |
| 10 | + |
| 11 | +### Problem Description |
| 12 | + |
| 13 | +You are given an array `nums` consisting of non-negative integers. You are also given a `queries` array, where `queries[i] = [xi, mi]`. |
| 14 | + |
| 15 | +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. |
| 16 | + |
| 17 | +Return an integer array `answer` where `answer.length == queries.length` and `answer[i]` is the answer to the ith query. |
| 18 | + |
| 19 | +### Example |
| 20 | + |
| 21 | +**Example 1:** |
| 22 | +``` |
| 23 | +Input: nums = [0, 1, 2, 3, 4], queries = [[3, 1], [1, 3], [5, 6]] |
| 24 | +Output: [3, 3, 7] |
| 25 | +``` |
| 26 | + |
| 27 | + |
| 28 | +**Explanation:** |
| 29 | +1. 0 and 1 are the only two integers not greater than 1. 0 XOR 3 = 3 and 1 XOR 3 = 2. The larger of the two is 3. |
| 30 | +2. 1 XOR 2 = 3. |
| 31 | +3. 5 XOR 2 = 7. |
| 32 | + |
| 33 | +**Example 2:** |
| 34 | +``` |
| 35 | +Input: nums = [5, 2, 4, 6, 6, 3], queries = [[12, 4], [8, 1], [6, 3]] |
| 36 | +Output: [15, -1, 5] |
| 37 | +``` |
| 38 | + |
| 39 | +### Constraints |
| 40 | + |
| 41 | +- 1 <= `nums.length`, `queries.length` <= 10^5 |
| 42 | +- `queries[i].length == 2` |
| 43 | +- 0 <= `nums[j]`, `xi`, `mi` <= 10^9 |
| 44 | + |
| 45 | +## Solution |
| 46 | + |
| 47 | +### Intuition |
| 48 | + |
| 49 | +To efficiently solve this problem, we can use a combination of sorting and Trie (prefix tree) to manage and query the maximum XOR. |
| 50 | + |
| 51 | +1. **Sort `nums` and `queries`**: Sort `nums` and `queries` based on the second element of each query (`mi`). This allows us to process each query in increasing order of `mi`, ensuring that we only consider valid `nums` elements for each query. |
| 52 | + |
| 53 | +2. **Use a Trie for Maximum XOR**: We will maintain a Trie of the binary representations of the numbers. For each query, we will insert numbers into the Trie that are less than or equal to `mi`, and then compute the maximum XOR for the current `xi`. |
| 54 | + |
| 55 | +### Time Complexity and Space Complexity Analysis |
| 56 | + |
| 57 | +- **Time Complexity**: |
| 58 | + - Sorting `nums` and `queries` takes $O(n \log n)$ and $O(q \log q)$ respectively. |
| 59 | + - Inserting each number into the Trie takes $O(31)$ for each number (as the maximum number of bits is 31). |
| 60 | + - Finding the maximum XOR for each query takes $O(31)$ for each query. |
| 61 | + - Overall time complexity is $O((n + q) \log(n + q))$. |
| 62 | + |
| 63 | +- **Space Complexity**: |
| 64 | + - The Trie will store up to `n` numbers, each taking $O(31)$ space. |
| 65 | + - The space complexity is $O(n \cdot 31)$. |
| 66 | + |
| 67 | +### Code |
| 68 | + |
| 69 | +#### C++ |
| 70 | + |
| 71 | +```cpp |
| 72 | +class Solution { |
| 73 | + struct TrieNode { |
| 74 | + TrieNode* children[2] = {}; |
| 75 | + }; |
| 76 | + |
| 77 | + TrieNode* root = new TrieNode(); |
| 78 | + |
| 79 | + void insert(int num) { |
| 80 | + TrieNode* node = root; |
| 81 | + for (int i = 30; i >= 0; --i) { |
| 82 | + int bit = (num >> i) & 1; |
| 83 | + if (!node->children[bit]) { |
| 84 | + node->children[bit] = new TrieNode(); |
| 85 | + } |
| 86 | + node = node->children[bit]; |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + int getMaxXOR(int num) { |
| 91 | + TrieNode* node = root; |
| 92 | + int maxXOR = 0; |
| 93 | + for (int i = 30; i >= 0; --i) { |
| 94 | + int bit = (num >> i) & 1; |
| 95 | + if (node->children[1 - bit]) { |
| 96 | + maxXOR |= (1 << i); |
| 97 | + node = node->children[1 - bit]; |
| 98 | + } else { |
| 99 | + node = node->children[bit]; |
| 100 | + } |
| 101 | + } |
| 102 | + return maxXOR; |
| 103 | + } |
| 104 | + |
| 105 | +public: |
| 106 | + vector<int> maximizeXor(vector<int>& nums, vector<vector<int>>& queries) { |
| 107 | + sort(nums.begin(), nums.end()); |
| 108 | + vector<pair<int, pair<int, int>>> q; |
| 109 | + int qLen = queries.size(); |
| 110 | + for (int i = 0; i < qLen; ++i) { |
| 111 | + q.push_back({queries[i][1], {queries[i][0], i}}); |
| 112 | + } |
| 113 | + sort(q.begin(), q.end()); |
| 114 | + |
| 115 | + vector<int> result(qLen); |
| 116 | + int idx = 0; |
| 117 | + for (const auto& [mi, xi_idx] : q) { |
| 118 | + int xi = xi_idx.first; |
| 119 | + int queryIdx = xi_idx.second; |
| 120 | + while (idx < nums.size() && nums[idx] <= mi) { |
| 121 | + insert(nums[idx]); |
| 122 | + ++idx; |
| 123 | + } |
| 124 | + result[queryIdx] = idx ? getMaxXOR(xi) : -1; |
| 125 | + } |
| 126 | + return result; |
| 127 | + } |
| 128 | +}; |
| 129 | +``` |
0 commit comments