<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>
struct Node {
    Node* links[2] = {NULL};
    bool flag = false;
    int cw = 0;
    int cp = 0;
};
class Trie {

public:
    Node* root;
    Trie() { root = new Node; }

    void insert(string word) {
        Node* node = root;
        for (char ch : word) {
            if (node->links[ch - '0'] == NULL) {
                node->links[ch - '0'] = new Node;
            }
            node = node->links[ch - '0'];
            node->cp++;
        }
        node->flag = true;
        node->cw++;
    }
    bool search(string word) {
        Node* node = root;
        for (char ch : word) {
            if (node->links[ch - '0'] == NULL) {
                return false;
            }
            node = node->links[ch - '0'];
        }
        if (node->flag == true)
            return true;
        else
            return false;
    }
    bool startsWith(string prefix) {
        Node* node = root;
        for (char ch : prefix) {
            if (node->links[ch - '0'] == NULL) {
                return false;
            }
            node = node->links[ch - '0'];
        }
        return true;
    }

    int countWordsEqualTo(string word) {
        // Write your code here.
        Node* node = root;
        for (char ch : word) {
            if (node->links[ch - '0'] == NULL) {
                return 0;
            }
            node = node->links[ch - '0'];
        }
        if (node->flag == true)
            return node->cw;
        else
            return 0;
    }
    int countWordsStartingWith(string word) {
        // Write your code here.
        Node* node = root;
        for (char ch : word) {
            if (node->links[ch - '0'] == NULL) {
                return 0;
            }
            node = node->links[ch - '0'];
        }
        return node->cp;
    }

    void erase(string word) {
        // Write your code here.
        Node* node = root;
        for (char ch : word) {
            if (node->links[ch - '0'] == NULL) {
                cout << "Word not found";
                return;
            }
            node = node->links[ch - '0'];
            node->cp--;
        }
        node->cw--;
    }
};

class Solution {
public:
    int findMaximumXOR(vector<int>& nums) {
        Trie t;
        int n = nums.size();
        for (int i = 0; i < n; i++) {
            bitset<32> binary(nums[i]);
            t.insert(binary.to_string());
        }
        int ans = INT_MIN;
        for (int i = 0; i < n; i++) {
            string maxi(32, '0');
            bitset<32> binary(nums[i]);
            string str = binary.to_string();
            Node* node = t.root;
            for (int j = 0; j < 32; j++) {
                if (str[j] == '1') {
                    if (node->links[0]) {
                        maxi[j] = '1';
                        node = node->links[0];
                    } else {
                        maxi[j] = '0';
                        node = node->links[1];
                    }
                } else // str[i]=='0'
                {
                    if (node->links[1]) {
                        maxi[j] = '1';
                        node = node->links[1];
                    } else {
                        maxi[j] = '0';
                        node = node->links[0];
                    }
                }
            }
            bitset<32> bitset(maxi);
            int deci = bitset.to_ulong();
            ans = max(ans, deci);
        }
        return ans;
    }
};
359128