## Similar Problems

Similar Problems not available

# Count Pairs With Xor In A Range - Leetcode Solution

## Companies:

LeetCode: Count Pairs With Xor In A Range Leetcode Solution

Difficulty: Hard

Topics: bit-manipulation array

The problem:

Count Pairs With Xor In A Range

Given an integer array nums, return the number of pairs of indices (i, j) such that i <= j and nums[i] XOR nums[j] between [low, high], inclusive.

Example 1:

Input: nums = [1,4,2,7], low = 2, high = 6 Output: 6 Explanation: All pairs with XOR in [2, 6] are:

- (0, 1): 1 XOR 4 = 5
- (0, 2): 1 XOR 2 = 3
- (0, 3): 1 XOR 7 = 6
- (1, 2): 4 XOR 2 = 6
- (1, 3): 4 XOR 7 = 3
- (2, 3): 2 XOR 7 = 5

Example 2:

Input: nums = [9], low = 0, high = 0 Output: 0

Constraints:

1 <= nums.length <= 2 * 10^4 1 <= nums[i] <= 2^31 - 1 0 <= low <= high <= 2^31 - 1

Solution:

The problem can be approached in multiple ways, but the most efficient one is to use the Trie data structure. Trie is a tree-like data structure that stores a collection of strings or sequences of characters, where each node represents a character of a given string. In the case of this problem, we will use a modified version of the Trie, called the XOR-Trie.

The XOR-Trie is a tree-like data structure that stores a collection of binary numbers, where each node represents a bit (0 or 1) of a given number. Each path from the root to a leaf node represents a binary number stored in the Trie. To insert a number into the XOR-Trie, we start from the root and insert each bit of the number one by one. At each level, we check if the corresponding bit is set or not. If it is not set, we create a new node and connect it to the current node. Otherwise, we move to the next level and repeat the same process.

To find all pairs with XOR between [low, high], we need to traverse the XOR-Trie and count the number of pairs whose XOR is within the given range. To do this, we need to traverse the XOR-Trie and at each node, we check if the XOR of the current prefix (up to that node) and the range [low, high] is within the given range. If it is, we add the count of the number of nodes in the XOR-Trie which has a prefix that forms a valid pair with the current prefix, and recursively traverse the tree. Otherwise, we prune the subtree and move to the next node.

Here's the step-by-step solution:

- Initialize a XOR-Trie with the root node.
- Traverse the input array nums from left to right and insert each number into the XOR-Trie.
- Traverse the XOR-Trie recursively and for each node: a) Compute the XOR of the current prefix (up to that node) and the range [low, high]. b) If the result is within the given range, add the count of the number of nodes in the XOR-Trie whose prefix XOR with the current prefix forms a valid pair, and recursively traverse the subtree. c) Otherwise, do not traverse the subtree and move to the next node.
- Return the total count of valid pairs.

Here's the Python code:

class TrieNode:
def **init**(self):
self.children = {}
self.is_num = False
self.is_leaf = False
self.count = 0 # number of leaves in the subtree

class XOR_Trie:
def **init**(self):
self.root = TrieNode()
self.root.is_leaf = True

```
def insert(self, num):
node = self.root
node.count += 1
for i in range(31, -1, -1):
bit = (num >> i) & 1
if bit not in node.children:
node.children[bit] = TrieNode()
node = node.children[bit]
node.count += 1
node.is_num = True
def count_pairs(self, node, prefix, low, high):
if node is None:
return 0
elif prefix ^ node.count < low or prefix > high:
return 0
count = 0
if node.is_num:
count += 1
for bit, child in node.children.items():
count += self.count_pairs(child, prefix ^ (bit << i), low, high)
return count
```

class Solution: def countPairs(self, nums: List[int], low: int, high: int) -> int: trie = XOR_Trie() for num in nums: trie.insert(num)

```
pairs = trie.count_pairs(trie.root, 0, low, high)
return pairs
```

The time complexity of the above algorithm is O(n log m), where n is the size of the input array nums and m is the maximum value of nums[i]. The space complexity is O(n log m).

## Count Pairs With Xor In A Range Solution Code

`1`