-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaxXor.cpp
More file actions
49 lines (44 loc) · 1 KB
/
MaxXor.cpp
File metadata and controls
49 lines (44 loc) · 1 KB
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
class TrieNode{
private:
int data;
public:
TrieNode*children[2];
TrieNode(int data){
this->data = data;
for(int i=0;i<2;i++){
children[i] = NULL;
}
}
};
class Trie{
private:
TrieNode*root;
public:
Trie(){
root = new TrieNode(-1);
}
void insertNumber(int num){
TrieNode*node = root;
for(int i=31;i>=0;i--){
int bit = (num>>i)&1;
if(node->children[bit]==NULL){
node->children[bit] = new TrieNode(bit);
}
node = node->children[bit];
}
}
int maxXor(int num){
int ans = 0;
TrieNode*node = root;
for(int i=31;i>=0;i--){
int bit = (num>>i)&1;
bit = !bit;
if(node->children[bit]){
ans|=(1<<i);
node = node->children[bit];
}
else node = node->children[!bit];
}
return ans;
}
};