<p>Given an integer array <code>nums</code>, return <em>the maximum result of </em><code>nums[i] XOR nums[j]</code>, where <code>0 &lt;= i &lt;= j &lt; n</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> nums = [3,10,5,25,2,8]
<strong>Output:</strong> 28
<strong>Explanation:</strong> The maximum result is 5 XOR 25 = 28.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> nums = [14,70,53,83,49,91,36,80,92,51,66,70]
<strong>Output:</strong> 127
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= nums.length &lt;= 2 * 10<sup>5</sup></code></li>
	<li><code>0 &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li>
</ul>

string num_to_string(int num){
    string s;
    while(num){
        int bit=(num&1);
        if(bit==0) s.push_back('0');
        else s.push_back('1');
        num>>=1;
    }
    while(s.size()<31) s.push_back('0');
    reverse(s.begin(),s.end());
    return s;
}
struct trie_node{
    trie_node* child[2];
    bool word_end;
    trie_node(){
        word_end=false;
        for(int i=0;i<2;i++)
        child[i]=nullptr;
    }
};
void insert_key(trie_node* root, int num){
    trie_node* curr=root;
    string s=num_to_string(num);
    for(char c:s){
        if(curr->child[c-'0']==nullptr){
            trie_node* new_node=new trie_node();
            curr->child[c-'0']=new_node;
        }
        curr=curr->child[c-'0'];
    }
    curr->word_end=true;
}
int max_xor(trie_node* root, int num){
    trie_node* curr=root;
    string s=num_to_string(num);
    for(int i=0;i<s.size();i++){
        if(s[i]=='1') s[i]='0';
        else s[i]='1';
    }
    int best=0,curr_num=0,i=30;
    for(char c:s){
        int bit=c-'0';
        if(curr->child[bit]==nullptr) bit=1-bit;
        if(curr->child[bit]==nullptr) break;
        if(bit==1) curr_num|=(1<<i);
        i--; curr=curr->child[bit];
        if(curr->word_end) best=max(best,curr_num^num);
    }
    return best;
}
class Solution {
public:
    int findMaximumXOR(vector<int>& nums) {
        trie_node* root=new trie_node();
        for(auto c:nums) insert_key(root,c);
        int ans=0;
        for(int i:nums){
            ans=max(ans,max_xor(root,i));
        }
        return ans;
    }
};
418618