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

const int LM = 31; // for bit Trie
class TrieNode {
    public:
    int data;
    bool isEnd;
    int endCnt;
    int preCnt;
    int k;
    vector<TrieNode*> child;
    
    TrieNode(int val, int K){
        data = val;
        k = K;
        isEnd = 0;
        endCnt = preCnt = 0;
        child.resize(k,0);
    }
};
class Trie {
    public:

    TrieNode* root = 0;
    int k;

    Trie(int K){
        k = K;
        root = new TrieNode(-1,k);
    }

    void insert(string &s){
        TrieNode *curr = root;
        for(auto it : s){
            curr->preCnt++;
            if(curr->child[it-'a'] == 0){
                TrieNode *temp = new TrieNode(it-'a',k);
                curr->child[it-'a'] = temp;
            }
            curr = curr->child[it-'a'];
        }
        curr->isEnd = 1;
        curr->endCnt++;
        curr->preCnt++;
    }

    void insert(int num){
        TrieNode *curr = root;
        for(int i=LM; i>=0; i--){
            curr->preCnt++;
            if((num>>i)&1){
                if(curr->child[1]==0){
                    TrieNode *temp = new TrieNode(1,k);
                    curr->child[1] = temp;
                }
                curr = curr->child[1];
            }
            else{
                if(curr->child[0]==0){
                    TrieNode *temp = new TrieNode(0,k);
                    curr->child[0] = temp;
                }
                curr = curr->child[0];
            }
        }
        curr->isEnd = 1;
        curr->endCnt++;
        curr->preCnt++;
    }

    bool find(string &s){
        TrieNode* head = root;
        for(int i=0; i<s.size(); i++){
            if(head->child[s[i]-'a']==0){
                return 0;
            }
            head = head->child[s[i]-'a'];
        }
        return head->isEnd;
    }

    bool find(int num){
        TrieNode* head = root;
        for(int i=LM; i>=0; i--){
            if(head->child[(num>>i)&1]==0){
                return 0;
            }
            head = head->child[(num>>i)&1];
        }
        return head->isEnd;
    }

    void erase(string &s){
        bool check = find(s);
        if(!check){
            return;
        }

        TrieNode* head = root;
        for(int i=0; i<s.size(); i++){
            head->preCnt--;
            head = head->child[s[i]-'a'];
        }
        head->preCnt--;
        head->endCnt--;
        if((head->endCnt)==0){
            head->isEnd = 0;
        }

    }

    void erase(int num){
        bool check = find(num);
        if(!check){
            return;
        }

        TrieNode* head = root;
        for(int i=LM; i>=0; i--){
            head->preCnt--;
            head = head->child[(num>>i)&1];
        }
        head->preCnt--;
        head->endCnt--;
        if((head->endCnt)==0){
            head->isEnd = 0;
        }

    }

    int countWords(string &s){
        TrieNode* head = root;
        for(int i=0; i<s.size(); i++){
            if(head->child[s[i]-'a']==0){
                return 0;
            }
            head = head->child[s[i]-'a'];
        }
        return head->endCnt;
    }

    int countWordsWithPrefix(string &s){
        TrieNode* head = root;
        for(int i=0; i<s.size(); i++){
            if(head->child[s[i]-'a']==0){
                return 0;
            }
            head = head->child[s[i]-'a'];
        }
        return head->preCnt;
    }

    int solve(int num){
        int ans = 0;
        TrieNode* head = root;
        for(int i=LM; i>=0; i--){
            if((num>>i)&1){
                // off
                if(head->child[0]){
                    ans |= (1<<i);
                    head = head->child[0];
                }
                else if(head->child[1]){
                    head = head->child[1];
                }
            }
            else{
                // on
                if(head->child[1]){
                    ans |= (1<<i);
                    head = head->child[1];
                }
                else if(head->child[0]){
                    head = head->child[0];
                }
            }
        }

        return ans;
    }


};

class Solution {
public:
    int findMaximumXOR(vector<int>& nums) {
        Trie ds(2);
        int ans = 0;
        for(int i=0; i<nums.size(); i++){
            ds.insert(nums[i]);
            int temp = ds.solve(nums[i]);
            ans = max(ans,temp);
        }
        return ans;
    }
};
428533