-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy path1707-maximum-xor-with-an-element-from-array.cpp
60 lines (58 loc) · 1.88 KB
/
1707-maximum-xor-with-an-element-from-array.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
class Solution {
struct node{
node *next[2];
int storeVal;
node(){
next[0] = NULL, next[1] = NULL, storeVal = 0;
}
};
void insert(node *root, int val)
{
for(int i=31;i>=0;i--)
{
bool res= (val & (1 << i));
if(root->next[res] == NULL)
root->next[res]=new node();
root=root->next[res];
}
root->storeVal = val;
}
int search(node *root, int val)
{
for(int i = 31; i >= 0; i--)
{
bool res = (val & (1 << i));
if(root->next[1 - res] != NULL)
root = root->next[1 - res];
else
root=root->next[res];
}
return (root->storeVal ^ val);
}
public:
vector<int> maximizeXor(vector<int>& nums, vector<vector<int>>& queries) {
int n = nums.size();
// To get the correct order after sorting
for(int i = 0; i < queries.size() ; i++)
queries[i].push_back(i);
// Sort the queries according to accending mi values
sort(queries.begin(),queries.end(), [](auto &a, auto &b){ return a[1] < b[1];});
sort(nums.begin(), nums.end());
int index = 0;
node * root = new node();
vector<int>res(queries.size(),-1);
for(auto q : queries)
{
//keep inserting all the values which are lesser than mi in the trie
while(index < n && nums[index] <= q[1])
{
insert(root, nums[index]);
index ++;
}
// if any value inserted then we are now in a state of taking xor with atleast any number which satifies the limit condition
if(index)
res[q[2]] = search(root, q[0]);
}
return res;
}
};