Count Pairs With XOR in a Range - Problem

Welcome to the world of XOR operations! In this challenging problem, you need to find pairs of numbers whose XOR result falls within a specific range.

Given an integer array nums and two integers low and high, your task is to count all "nice pairs" in the array.

A nice pair is defined as a pair (i, j) where:

  • 0 <= i < j < nums.length (i comes before j)
  • low <= (nums[i] XOR nums[j]) <= high (XOR result is within range)

Example: If nums = [1, 4, 2, 7], low = 2, and high = 6, then pair (1, 4) has XOR value 1 โŠ• 4 = 5, which is within range [2, 6].

The challenge lies in efficiently computing XOR values for all pairs while staying within time limits for large arrays.

Input & Output

example_1.py โ€” Basic Case
$ Input: nums = [1, 4, 2, 7], low = 2, high = 6
โ€บ Output: 6
๐Ÿ’ก Note: All pairs have XOR values in range: (1,4)โ†’5, (1,2)โ†’3, (1,7)โ†’6, (4,2)โ†’6, (4,7)โ†’3, (2,7)โ†’5. All 6 pairs are valid.
example_2.py โ€” Narrow Range
$ Input: nums = [9, 8, 4, 2, 1], low = 5, high = 14
โ€บ Output: 8
๐Ÿ’ก Note: Valid pairs: (9,8)โ†’1โŒ, (9,4)โ†’13โœ“, (9,2)โ†’11โœ“, (9,1)โ†’8โœ“, (8,4)โ†’12โœ“, (8,2)โ†’10โœ“, (8,1)โ†’9โœ“, (4,2)โ†’6โœ“, (4,1)โ†’5โœ“, (2,1)โ†’3โŒ. Count = 8.
example_3.py โ€” Edge Case
$ Input: nums = [1, 1, 1], low = 0, high = 0
โ€บ Output: 3
๐Ÿ’ก Note: All pairs (1,1) have XOR = 0, which equals both low and high bounds. Pairs: (0,1), (0,2), (1,2) all give XOR = 0.

Visualization

Tap to expand
๐Ÿ” The Binary Lock Security SystemSecurity Keys001100010111Keys: [1,4,2,7]XOR Combination ProcessKey 1 (001) โŠ• Key 4 (100) = 101 (5)Key 1 (001) โŠ• Key 2 (010) = 011 (3)Key 1 (001) โŠ• Key 7 (111) = 110 (6)Key 4 (100) โŠ• Key 2 (010) = 110 (6)... and so onSecurity Range ValidatorAcceptable Range: [2, 6]โœ“ Code 5 (101) - VALIDโœ“ Code 3 (011) - VALIDโœ“ Code 6 (110) - VALIDTotal Valid Combinations: 6Brute ForceCheck every pairO(nยฒ) timeโ†’Trie OptimizationSmart bit matchingO(nร—log V) time๐ŸŽฏ Trie Magic Explainedโ€ข Build prefix tree of binary representationsโ€ข For each key, traverse tree to count valid combinationsโ€ข Use range query: count(โ‰คhigh) - count(โ€ข Avoid checking every pair individually!๐Ÿ”‘ Key Security InsightThe Trie data structure acts like an intelligent security system that can predictwhich key combinations will produce valid codes WITHOUT testing every single pair.It's like having a smart lock that knows the answer before you even insert the second key!
Understanding the Visualization
1
Prepare Keys
Convert each number to its binary representation - these are our security keys
2
Smart Matching
Use a Trie to efficiently find which key combinations produce codes in our target range
3
Count Valid Codes
For each key, query the Trie to count how many previous keys would create valid security codes
Key Takeaway
๐ŸŽฏ Key Insight: By representing numbers in binary and using a Trie, we can efficiently navigate through bit patterns to count XOR pairs within any range, transforming a quadratic problem into a near-linear solution.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n * log(MAX_VAL))

Each number requires O(log(MAX_VAL)) time for Trie operations, done for n numbers

n
2n
โšก Linearithmic
Space Complexity
O(n * log(MAX_VAL))

Trie stores up to n numbers, each with log(MAX_VAL) bits

n
2n
โšก Linearithmic Space

Constraints

  • 1 โ‰ค nums.length โ‰ค 2 ร— 104
  • 1 โ‰ค nums[i] โ‰ค 2 ร— 104
  • 1 โ‰ค low โ‰ค high โ‰ค 2 ร— 104
  • XOR operations are bitwise exclusive OR
Asked in
Google 34 Amazon 28 Meta 22 Microsoft 18
23.4K Views
Medium-High Frequency
~35 min Avg. Time
987 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen