All practice tasks

Coding (Algo)

Count Similar Configuration Pairs

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

2

Example 2

{
  "k": 0,
  "nums": [
    7,
    7,
    7
  ]
}

Returns

3

Required complexity

O(32n²) time and O(1) auxiliary space

What this task practices

Count Similar Configuration Pairs is a coding (algo) interview exercise that trains prompt interpretation, explicit assumptions, a concrete response, and a clear explanation of tradeoffs. The catalog marks it as easy difficulty. It focuses on Technical Reasoning, Testing, Validation. The signed-in workspace provides the tools for the round and evaluates the attempt against task-specific criteria. Reference solutions, hidden checks, evaluator instructions, and candidate work remain private.

Sign in to startThe workspace and evaluation open after sign in.