# 2857. Count Pairs of Points With Distance k

## Problem Statement

<br>

You are given a **2D** integer array `coordinates` and an integer `k`, where `coordinates[i] = [xi, yi]` are the coordinates of the `ith` 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`.

&#x20;

**Example 1:**

<pre><code><strong>Input: coordinates = [[1,2],[4,2],[1,3],[5,2]], k = 5
</strong><strong>Output: 2
</strong><strong>Explanation: We can choose the following pairs:
</strong>- (0,1): Because we have (1 XOR 4) + (2 XOR 2) = 5.
- (2,3): Because we have (1 XOR 5) + (3 XOR 2) = 5.
</code></pre>

**Example 2:**

<pre><code><strong>Input: coordinates = [[1,3],[1,3],[1,3],[1,3],[1,3]], k = 0
</strong><strong>Output: 10
</strong><strong>Explanation: Any two chosen pairs will have a distance of 0. There are 10 ways to choose two pairs.
</strong></code></pre>

&#x20;

**Constraints:**

* `2 <= coordinates.length <= 50000`
* `0 <= xi, yi <= 106`
* `0 <= k <= 100`

## Intuition

```
Approach:

Since K constraint is less,
We can iterate throught the k

x1^x2 = i , y1^y2 = k-i
a^b = c -> b = c^a

Go through all k's


```

### Links

<https://leetcode.com/problems/count-pairs-of-points-with-distance-k/description/>

### Video Links

<https://www.youtube.com/watch?v=1Hp6Hsghtno&ab_channel=AryanMittal>

### Approach 1:

```
```

{% code title="C++" lineNumbers="true" %}

```cpp
class Solution {
public:
    int countPairs(vector<vector<int>>& arr, int k) {
        map<pair<int,int> , int> mp;
        int ans = 0;
        for(auto &it: arr){
            int x1 = it[0], y1 = it[1];
            for(int i=0; i<=k; i++){
                int x2 = x1^i;
                int y2 = y1^(k-i);

                ans += mp[{x2,y2}];
            }

            mp[{x1,y1}]++;
        }

        return ans;
    }
};
```

{% endcode %}

### Approach 2:

```
```

{% code title="C++" lineNumbers="true" %}

```cpp
```

{% endcode %}

### Approach 3:

```
```

{% code title="C++" lineNumbers="true" %}

```cpp
```

{% endcode %}

### Approach 4:

```
```

{% code title="C++" lineNumbers="true" %}

```cpp
```

{% endcode %}

### Similar Problems

###


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://coding-9.gitbook.io/untitled/bit-masking/2857.-count-pairs-of-points-with-distance-k.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
