LeetCode 1803: Count Pairs With XOR in a Range

LeetCode 1803 Solution 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.

:

LeetCode 1803 Solutions in Java, C++, Python

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

Interactive Code Editor for LeetCode 1803

Improve Your LeetCode 1803 Solution

Use the editor below to refine the provided solution for LeetCode 1803. Select a programming language and try the following:

  • Add import statements if required.
  • Optimize the code for better time or space complexity.
  • Add test cases to validate edge cases and common scenarios.
  • Handle error conditions or invalid inputs gracefully.
  • Experiment with alternative approaches to deepen your understanding.

Click "Run Code" to execute your solution and view the output. If errors occur, check the line numbers and debug accordingly. Resize the editor by dragging its bottom edge.

Loading editor...

Related LeetCode Problems