You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
structTrieNode {
TrieNode *next[2] = {};
};
classSolution {
voidadd(TrieNode *node, int n) {
for (int i = 31; i >= 0; --i) {
int b = n >> i & 1;
if (node->next[b] == NULL) node->next[b] = newTrieNode();
node = node->next[b];
}
}
intmaxXor(TrieNode *node, int n) {
int ans = 0;
for (int i = 31; i >= 0; --i) {
int b = n >> i & 1;
if (node->next[1 - b]) { // if we can go the opposite direction, do it.
node = node->next[1 - b];
ans |= 1 << i;
} else {
node = node->next[b];
}
}
return ans;
}
public:intfindMaximumXOR(vector<int>& A) {
TrieNode root;
int ans = 0;
for (int n : A) {
add(&root, n);
ans = max(ans, maxXor(&root, n));
}
return ans;
}
};