<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:
    const static int N=31*2*1e5, M=2;
    int trie[N][M]; int nexti=1;

    void build(string& s1){
        int curr=0;
        for(int i=0; i<s1.size(); i++){
            if(trie[curr][s1[i]-'0']==-1) curr=trie[curr][s1[i]-'0']=nexti++;
            else curr=trie[curr][s1[i]-'0'];
        }
    }

    void init(){
        for(int i=0; i<N; i++){
            for(int j=0; j<M; j++){
                trie[i][j]=-1;
            }
        }
    }
    int findMaximumXOR(vector<int>& nums) {
        init();
        int n=nums.size();

        vector<string> v1(n);
        for(int i=0; i<n; i++){
            bitset<32> b1(nums[i]);
            v1[i]=b1.to_string();
            build(v1[i]);
        }

        int ans=0;
        for(int i=0; i<n; i++){
            int curr=0;
            int ansi=0;
            for(int j=0; j<v1[i].size(); j++){
                if(trie[curr][((v1[i][j]-'0')+1)%2]!=-1){
                    ansi+=(1ll<<(31-j));
                    curr=trie[curr][((v1[i][j]-'0')+1)%2];
                }
                else{
                    curr=trie[curr][v1[i][j]-'0'];
                }
            }
            ans=max(ans, ansi);
        }
        return ans;
    }
};
145956