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

struct TrieNode{
    TrieNode *children[2];
    bool flag;
    
    TrieNode () {
        children[0] = nullptr;
        children[1] = nullptr;
        flag = false;
    }
};

class Solution {
public:
    void dfs(TrieNode *left, TrieNode *right, int currNum) {
        if (!left || !right) return;
        maxXOR = max(maxXOR, currNum);
        if (left->flag) return;
        int newNum = (currNum << 1);
        bool diffExists = false;
        if (left->children[0] && right->children[1]) {
            dfs(left->children[0], right->children[1], newNum+1);
            diffExists = true;
        }
        if (right != left && left->children[1] && right->children[0]) {
            dfs(left->children[1], right->children[0], newNum+1);
            diffExists = true;
        }
        
        if (!diffExists) {
            if (left->children[0] && right->children[0]) {
                dfs(left->children[0], right->children[0], newNum);
            }
            if (left->children[1] && right->children[1]) {
                dfs(left->children[1], right->children[1], newNum);
            }
        }
    }
    
    int findMaximumXOR(vector<int>& nums) {
        constructTrie(nums);
        dfs(root, root, 0);
        
        return maxXOR;
    }
    
private:
    TrieNode *root;
    int maxXOR = 0;
    
    int index(char c) {
        return c-'0';
    }
    
    void insert(string &s) {
        TrieNode *curr = root;
        for (char c : s) {
            if (!curr->children[index(c)]) curr->children[index(c)] = new TrieNode();
            curr = curr->children[index(c)];
        }
        curr->flag = true;
    }
    
    void constructTrie(vector<int>& nums) {
        int n = nums.size();
        vector<string> numsBinary(n);
        for(int i = 0; i < n; i++) {
            numsBinary[i] = decToBinary(nums[i]);
        }
        addPadding(numsBinary);
        root = new TrieNode();
        for (int i = 0; i < n; i++) {
            insert(numsBinary[i]);
        }
    }
    
    string decToBinary(int x) {
        string res;
        while (x > 0) {
            res += (x & 1) ? '1' : '0';
            x = x >> 1;
        }
        reverse(res.begin(), res.end());
        return res;
    }
    
     void addPadding(vector<string> &numsBinary) {
         int maxLen = 0;
         for (string &s : numsBinary) {
             maxLen = max(maxLen, (int)s.length());
         }
         
         for (string &s : numsBinary) {
             while (s.length() < maxLen) {
                 s = '0' + s;
             }
         }
     }
};
324426