Count Pairs Whose XOR Has Exactly K Set Bits
Math & Bit ManipulationAuto-graded · 17 test casesLast updated
Tests the ability to combine bit counting (popcount) with efficient pair enumeration to find pairs in an array whose XOR contains exactly K set bits.
Count Pairs Whose XOR Has Exactly K Set Bits
Given a list of non-negative integers nums and a non-negative integer k, count the number of unordered pairs (i, j) with i < j such that the bitwise XOR of nums[i] and nums[j] has exactly k set bits (i.e., its popcount equals k).
Background
The popcount (population count) of an integer is the number of 1-bits in its binary representation. For example:
popcount(0b1011)= 3popcount(0b1100)= 2
The XOR of two integers flips every bit position where the two values differ. So popcount(a XOR b) counts the number of bit positions where a and b differ.
Task
Implement count_xor_pairs(nums, k) that returns the count of unordered pairs (i, j) with i < j such that bin(nums[i] ^ nums[j]).count('1') == k.
Constraints
0 ≤ len(nums) ≤ 20000 ≤ nums[i] ≤ 10^60 ≤ k ≤ 20- Pairs are unordered:
(i, j)and(j, i)count as one pair. - If
len(nums) < 2, return0. - Duplicate values in
numsare allowed; each index is treated as a distinct element.
Examples
Example 1
Input: count_xor_pairs([3, 5, 7, 1], 2)
Output: 2
Example 2
Input: count_xor_pairs([0, 0, 0], 0)
Output: 3
Example 3
Input: count_xor_pairs([1, 2, 4, 8], 2)
Output: 6
Hints
Hint 1
XOR of two numbers has a 1-bit exactly where the two numbers differ; popcount(a ^ b) counts those differing positions.
Hint 2
A brute-force O(n²) scan over all pairs is sufficient for n ≤ 2000; Python's int.bit_count() (3.10+) or bin(x).count('1') gives popcount in O(1) for bounded values.
Hint 3
Group values by their popcount in a frequency map, then combine groups whose popcounts can XOR to give exactly k set bits — but be careful: popcount(a ^ b) ≠ popcount(a) XOR popcount(b), so straightforward grouping alone does not work without extra care.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Core Observation
For any two integers a and b, a ^ b produces a value whose set bits mark every position where a and b differ. So bin(a ^ b).count('1') counts exactly how many bit positions differ between a and b. We want this count to equal k.
Algorithm
Because n ≤ 2000, there are at most 2000 × 1999 / 2 = 1,999,000 pairs — well within a straightforward O(n²) brute-force budget.
- Iterate over all pairs
(i, j)withi < j. - Compute
xor_val = nums[i] ^ nums[j]. - Compute its popcount via
bin(xor_val).count('1')(orxor_val.bit_count()in Python 3.10+). - If the popcount equals
k, increment the counter. - Return the counter.
Why It Is Correct
Every unordered pair is visited exactly once (the inner loop starts at i + 1), and the popcount check is exact. No pair is double-counted.
Complexity
- Time: O(n²) — at most ~2 × 10⁶ iterations, each O(1) for integers bounded by 10⁶ (≤ 20 bits).
- Space: O(1) — only a counter is maintained.
Note on a Tempting Shortcut
One might try to group elements by their own popcount and combine groups — but popcount(a ^ b) is not a simple function of popcount(a) and popcount(b) alone. For example, a = 0b11 and b = 0b11 both have popcount 2, but a ^ b = 0 has popcount 0. This means such grouping does not eliminate the need to examine pairs individually without additional structure.
from typing import List
def count_xor_pairs(nums: List[int], k: int) -> int:
count = 0
n = len(nums)
for i in range(n):
for j in range(i + 1, n):
if bin(nums[i] ^ nums[j]).count('1') == k:
count += 1
return count