LeetCode 1707: Maximum XOR With an Element From Array Solution

Master LeetCode problem 1707 (Maximum XOR With an Element From Array), a hard challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

1707. Maximum XOR With an Element From Array

Problem Explanation

Explanation

To solve this problem efficiently, we can use a Trie data structure to store the binary representations of the numbers in nums. We will build the Trie such that each bit of a number is represented as a node in the Trie. Then, for each query [xi, mi], we can traverse the Trie to find the maximum XOR value that satisfies the condition nums[j] <= mi.

Algorithmic Idea:

  1. Build a Trie using the binary representations of the numbers in nums.
  2. For each query [xi, mi]: a. Traverse the Trie to find the maximum XOR value that satisfies the condition nums[j] <= mi.

Time Complexity:

  • Building the Trie: O(N * K) where N is the number of elements in nums and K is the maximum number of bits.
  • Processing each query: O(K) where K is the maximum number of bits.
  • Overall time complexity: O(N * K + Q * K) where Q is the number of queries.

Space Complexity:

  • O(N * K) to store the Trie nodes and values.

Solution Code

class TrieNode {
    TrieNode[] children;
    int value;

    TrieNode() {
        children = new TrieNode[2];
        value = -1;
    }
}

class Solution {
    public int[] maximizeXor(int[] nums, int[][] queries) {
        TrieNode root = new TrieNode();
        for (int num : nums) {
            TrieNode curr = root;
            for (int i = 31; i >= 0; i--) {
                int bit = (num >> i) & 1;
                if (curr.children[bit] == null) {
                    curr.children[bit] = new TrieNode();
                }
                curr = curr.children[bit];
            }
            curr.value = num;
        }

        int[] result = new int[queries.length];
        for (int i = 0; i < queries.length; i++) {
            int xi = queries[i][0];
            int mi = queries[i][1];
            int maxXor = 0;
            TrieNode curr = root;

            for (int j = 31; j >= 0; j--) {
                int bit = (xi >> j) & 1;
                if (curr.children[bit ^ 1] != null && (curr.children[bit ^ 1].value <= mi)) {
                    maxXor += (1 << j);
                    curr = curr.children[bit ^ 1];
                } else {
                    curr = curr.children[bit];
                }
            }
            result[i] = maxXor;
        }

        return result;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 1707 (Maximum XOR With an Element From Array)?

This page provides optimized solutions for LeetCode problem 1707 (Maximum XOR With an Element From Array) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 1707 (Maximum XOR With an Element From Array)?

The time complexity for LeetCode 1707 (Maximum XOR With an Element From Array) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 1707 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 1707 in Java, C++, or Python.

Back to LeetCode Solutions