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

#include <bits/stdc++.h>
using namespace std;
using ints=int;
using doublee=double;
using vi=vector<ints>;
using pi=pair<ints,ints>;
using vvi=vector<vector<ints>>;
using vpi=vector<pi>;
#define endl       "\n"
#define all(arr) arr.begin(),arr.end()

struct node{
    ints prefixCount,wordCount;
    node* children[2];

    node(ints prefixCount) {
        this->prefixCount=prefixCount;
        this->wordCount=0;
        for(ints i=0;i<2;i++) 
            children[i]=nullptr;
    }
};

class Trie{
    public:
        ints ct;
        node *root;

        Trie() {
            root= new node(0);
            ct=0;
        }

        void insert(string &s) {
            ints m=s.size();
            this->ct++;
            node *ptr=this->root;
            for(ints i=0;i<m;i++) {
                ints ch=s[i]-'0';
                if(ptr->children[ch]==nullptr) {
                    node* newNode=new node(1);
                    ptr->children[ch]=newNode;
                }
                else{
                    ptr->children[ch]->prefixCount++;
                }
                ptr=ptr->children[ch];
            }
            ptr->wordCount++;
        }
};

class Solution {
public:
    ints sol(node* root1,node* root2,ints level) {
        if(root1==nullptr) return 0;
        ints temp=0,maxx=0;
        bool ff=0;
        if(root1->children[0]) {
            if(root2->children[1]) 
                ff=1,temp=(1ll<<level) + sol(root1->children[0],root2->children[1],level-1);
            else
                temp=sol(root1->children[0],root2->children[0],level-1);
            maxx=max(maxx,temp);
        }
        if(root1->children[1]) {
            if(root2->children[0]) 
                temp=(1ll<<level) + sol(root1->children[1],root2->children[0],level-1);
            else if(!ff)
                temp=sol(root1->children[1],root2->children[1],level-1);
            maxx=max(maxx,temp);
        }
        return maxx;
    }

    ints findMaximumXOR(vi &arr) {
        ints n=arr.size();
        Trie *trie=new Trie();
        for(ints i=0;i<n;i++) {
            string t;
            for(ints j=30;j>=0;j--) {
                if((1ll<<j)&arr[i]) t+='1';
                else t+='0';
            }
            trie->insert(t);
        }
        node *ptr=trie->root;
        ints ans=0,level=30;
        while(true) {
            if(ptr->children[0] && ptr->children[1]){
                ans=(1ll<<level);
                break;
            } 
            else if(ptr->children[0]) ptr=ptr->children[0];
            else if(ptr->children[1]) ptr=ptr->children[1];
            else break;
            level--;
        }
        ans+= sol(ptr->children[0],ptr->children[1],level-1);
        return ans;
    }
};
334341