<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>

class Solution {
public:
   struct trie{
    trie *next[2];
   };
   trie* getNode(){
    trie *curr= new trie();
  curr->next[0]=NULL;
  curr->next[1]=NULL;
return curr;
   }
   void insert(trie *&root,int num){
    int pos=0;
    int arr[32]={0};
    while(num){
        arr[32-pos-1]=num&1;
        num>>=1;
        pos++;
    }
    trie *curr=root;
    for(int i=0;i<32;i++){
        int x=arr[i];
        if(curr->next[x])
        curr=curr->next[x];
        else{
            curr->next[x]=getNode();
            curr=curr->next[x];
        }
    }
   }
   void solve(trie* &root,int num,int &ans){
    int val=0;
    trie* curr=root;
    int pos=0;
    int arr[32]={0};
    while(num){
        arr[32-pos-1]=num&1;
        num>>=1;
        pos++;
    }
    for(int i=0;i<32;i++){
        int x=arr[i];
        int y=x^1;
        if(curr->next[y]){
            curr=curr->next[y];
            val |= 1<<(32-i-1);
        }
        else{
            curr=curr->next[x];
        }
    }
    ans=max(ans,val);
   }
    int findMaximumXOR(vector<int>& nums) {
        trie *root=getNode();
        int ans=0;
        for(int i=0;i<nums.size();i++){
            if(i>0)
            solve(root,nums[i],ans);
            insert(root,nums[i]);
        }
        return ans;
    }
};
185616