Bit manipulation · Easy
A diagnostic compares small batches of 32-bit configuration masks. Two records are similar when they differ in at most k flag positions. The batch is deliberately bounded so explicit pairwise comparison is sufficient and easier to verify.
Count unordered pairs of distinct record indexes whose masks differ in at most k bits.
Input
Call
- Implement solve(input) and return the result. The provided adapter handles JSON input and output.
Input shape
{ nums: unsigned32[], k: integer }
Fields
- nums contains unsigned 32-bit integers from 0 through 4294967295. Values with bit 31 set are positive JSON integers.
- k is an integer from 0 through 32 specifying the inclusive maximum number of differing bits.
Output
Return value
- Return the number of index pairs i < j. Never compare a record with itself or count both pair orders.
- Equal values at distinct indexes form a valid distance-zero pair. Fewer than two records returns zero.
Constraints
- 0 <= nums.length <= 2000; 0 <= k <= 32.
Examples
Example 1
{
"k": 1,
"nums": [
0,
1,
3
]
}Returns
2Example 2
{
"k": 0,
"nums": [
7,
7,
7
]
}Returns
3Required complexity
O(32n²) time and O(1) auxiliary space