Count Pairs of Points With Distance k

You are given a 2D integer array coordinates and an integer k, where coordinates[i] = [xi, yi] are the coordinates of the i^th point in a 2D plane.

We define the distance between two points (x1, y1) and (x2, y2) as (x1 XOR x2) + (y1 XOR y2), where XOR is the bitwise XOR operation.

Return the number of pairs (i, j) such that i < j and the distance between points i and j is equal to k.

Example 1
Inputcoordinates = [[1,2],[4,2],[1,3],[5,2]], k = 5
Output2
Pairs (0,1) and (2,3) both have XOR distance 5.
Example 2
Inputcoordinates = [[1,3],[1,3],[1,3],[1,3],[1,3]], k = 0
Output10
Any two points have distance 0, and there are 10 ways to choose two of the five points.

Constraints

  • 2 <= coordinates.length <= 50000
  • 0 <= xi, yi <= 10^6
  • 0 <= k <= 100

Asked at 1 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate