LeetCode 1803: Count Pairs With XOR in a Range Solution

Master LeetCode problem 1803 (Count Pairs With XOR in a Range), 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.

1803. Count Pairs With XOR in a Range

Problem Explanation

Explanation:

To solve this problem, we can iterate through all pairs of indices (i, j) where i < j, calculate the XOR of nums[i] and nums[j], and check if this XOR value falls within the given range [low, high]. If it does, we increment the count of nice pairs.

We can optimize this process by using a trie data structure to store the binary representations of the elements in nums. This allows us to efficiently find pairs that produce XOR values within the specified range. We can then traverse the trie to count the valid pairs.

The algorithm involves constructing a trie from the binary representations of the elements in nums. Then, for each element in nums, we traverse the trie to find the XOR values that satisfy the given range.

Steps:

  1. Construct a trie from the binary representations of the elements in nums.
  2. For each element in nums, calculate the XOR value by traversing the trie.
  3. Count the valid pairs where the XOR value falls within the given range.
  4. Return the total count of nice pairs found.

Time Complexity:

The time complexity of this algorithm is O(N * log(MAX)) where N is the number of elements in nums and MAX is the maximum value in nums. This complexity arises from constructing the trie and traversing it to find valid pairs.

Space Complexity:

The space complexity of this algorithm is O(N * log(MAX)) to store the trie and its nodes.

:

Solution Code

class TrieNode {
    TrieNode[] children;

    public TrieNode() {
        children = new TrieNode[2];
    }
}

class Solution {
    public int countPairs(int[] nums, int low, int high) {
        TrieNode root = new TrieNode();
        int count = 0;

        for (int num : nums) {
            count += search(root, num, high + 1) - search(root, num, low);
            insert(root, num);
        }

        return count;
    }

    private void insert(TrieNode root, int num) {
        TrieNode curr = root;

        for (int i = 14; i >= 0; i--) {
            int bit = (num >> i) & 1;

            if (curr.children[bit] == null) {
                curr.children[bit] = new TrieNode();
            }

            curr = curr.children[bit];
        }
    }

    private int search(TrieNode root, int num, int limit) {
        TrieNode curr = root;
        int count = 0;

        for (int i = 14; i >= 0; i--) {
            if (curr == null) {
                break;
            }

            int bitNum = (num >> i) & 1;
            int bitLimit = (limit >> i) & 1;

            if (bitLimit == 1) {
                if (curr.children[bitNum] != null) {
                    curr = curr.children[bitNum];
                } else {
                    break;
                }
            } else {
                if (curr.children[1 - bitNum] != null) {
                    count += (1 << i);
                    curr = curr.children[1 - bitNum];
                } else {
                    curr = curr.children[bitNum];
                }
            }
        }

        return count;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 1803 (Count Pairs With XOR in a Range)?

This page provides optimized solutions for LeetCode problem 1803 (Count Pairs With XOR in a Range) 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 1803 (Count Pairs With XOR in a Range)?

The time complexity for LeetCode 1803 (Count Pairs With XOR in a Range) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 1803 on DevExCode?

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

Back to LeetCode Solutions