<p>You are given an array <code>nums</code> consisting of non-negative integers. You are also given a <code>queries</code> array, where <code>queries[i] = [x<sub>i</sub>, m<sub>i</sub>]</code>.</p>

<p>The answer to the <code>i<sup>th</sup></code> query is the maximum bitwise <code>XOR</code> value of <code>x<sub>i</sub></code> and any element of <code>nums</code> that does not exceed <code>m<sub>i</sub></code>. In other words, the answer is <code>max(nums[j] XOR x<sub>i</sub>)</code> for all <code>j</code> such that <code>nums[j] &lt;= m<sub>i</sub></code>. If all elements in <code>nums</code> are larger than <code>m<sub>i</sub></code>, then the answer is <code>-1</code>.</p>

<p>Return <em>an integer array </em><code>answer</code><em> where </em><code>answer.length == queries.length</code><em> and </em><code>answer[i]</code><em> is the answer to the </em><code>i<sup>th</sup></code><em> query.</em></p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> nums = [0,1,2,3,4], queries = [[3,1],[1,3],[5,6]]
<strong>Output:</strong> [3,3,7]
<strong>Explanation:</strong>
1) 0 and 1 are the only two integers not greater than 1. 0 XOR 3 = 3 and 1 XOR 3 = 2. The larger of the two is 3.
2) 1 XOR 2 = 3.
3) 5 XOR 2 = 7.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> nums = [5,2,4,6,6,3], queries = [[12,4],[8,1],[6,3]]
<strong>Output:</strong> [15,-1,5]
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= nums.length, queries.length &lt;= 10<sup>5</sup></code></li>
	<li><code>queries[i].length == 2</code></li>
	<li><code>0 &lt;= nums[j], x<sub>i</sub>, m<sub>i</sub> &lt;= 10<sup>9</sup></code></li>
</ul>

class Node{
    vector<Node *> links;
public:
    
    Node() {
        links.assign(2, NULL);
    }

    bool hasKey(int bit) {
        return links[bit] != NULL;
    }

    Node *getKey(int bit) {
        return links[bit];
    }

    void createKey(int bit) {
        links[bit] = new Node();
    }
};

class Trie{
    Node *root;

public:

    Trie() {
        root = new Node();
    }

    void addNumber(int n) {
        bitset<32> num(n);

        Node *node = root;

        for(int i=31; i>=0; i--) {
            if(!node->hasKey(num[i])) {
                node->createKey(num[i]);
            }
            node = node->getKey(num[i]);
        }
        return;
    }

    int getMaxXOR(int n) {
        bitset<32> num(n);
        int maxXOR = 0;

        Node *node = root;

        for(int i=31; i>=0; i--) {
            if(node->hasKey(!num[i])) {
                maxXOR += (1 << i);
                node = node->getKey(!num[i]);
            } else {
                node = node->getKey(num[i]);
            }
        }

        return maxXOR;
    }
};


class Solution {

    static bool compareQuery(vector<int> &q1, vector<int> &q2) {
        return q1[1] < q2[1];
    }

    vector<vector<int>> getOfflineQueries(vector<vector<int>>& queries) {
        int n = queries.size();

        vector<vector<int>> queriesWithIndex;

        for(int i=0; i<n; i++) {
            vector<int> query = queries[i];
            query.push_back(i);
            queriesWithIndex.push_back(query);
        }
        sort(queriesWithIndex.begin(), queriesWithIndex.end(), compareQuery);
        return queriesWithIndex;
    }

public:
    vector<int> maximizeXor(vector<int>& nums, vector<vector<int>>& queries) {
        Trie tree = Trie();

        sort(nums.begin(), nums.end());

        vector<vector<int>> queriesWithIndex = getOfflineQueries(queries);

        int n = nums.size(), q = queries.size();
        int i = 0;

        vector<int> ans(q, -1);

        for(vector<int> &query: queriesWithIndex) {
            int x = query[0], m = query[1], index = query[2];

            while(i < n && nums[i] <= m) {
                tree.addNumber(nums[i++]);
            }

            // No number less than m.
            if(i == 0) continue;

            ans[index] = tree.getMaxXOR(x);
        }

        return ans;
    }
};
417726