Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
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.
Example 1:
Input: coordinates = [[1,2],[4,2],[1,3],[5,2]], k = 5 Output: 2 Explanation: We can choose the following pairs: - (0,1): Because we have (1 XOR 4) + (2 XOR 2) = 5. - (2,3): Because we have (1 XOR 5) + (3 XOR 2) = 5.
Example 2:
Input: coordinates = [[1,3],[1,3],[1,3],[1,3],[1,3]], k = 0 Output: 10 Explanation: Any two chosen pairs will have a distance of 0. There are 10 ways to choose two pairs.
Constraints:
2 <= coordinates.length <= 500000 <= xi, yi <= 1060 <= k <= 100Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Hash Map · Bit Manipulation
[[1,2],[4,2],[1,3],[5,2]] 5
[[1,3],[1,3],[1,3],[1,3],[1,3]] 0
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2857: Count Pairs of Points With Distance k
class Solution {
public int countPairs(List<List<Integer>> coordinates, int k) {
Map<List<Integer>, Integer> cnt = new HashMap<>();
int ans = 0;
for (var c : coordinates) {
int x2 = c.get(0), y2 = c.get(1);
for (int a = 0; a <= k; ++a) {
int b = k - a;
int x1 = a ^ x2, y1 = b ^ y2;
ans += cnt.getOrDefault(List.of(x1, y1), 0);
}
cnt.merge(c, 1, Integer::sum);
}
return ans;
}
}
// Accepted solution for LeetCode #2857: Count Pairs of Points With Distance k
func countPairs(coordinates [][]int, k int) (ans int) {
cnt := map[[2]int]int{}
for _, c := range coordinates {
x2, y2 := c[0], c[1]
for a := 0; a <= k; a++ {
b := k - a
x1, y1 := a^x2, b^y2
ans += cnt[[2]int{x1, y1}]
}
cnt[[2]int{x2, y2}]++
}
return
}
# Accepted solution for LeetCode #2857: Count Pairs of Points With Distance k
class Solution:
def countPairs(self, coordinates: List[List[int]], k: int) -> int:
cnt = Counter()
ans = 0
for x2, y2 in coordinates:
for a in range(k + 1):
b = k - a
x1, y1 = a ^ x2, b ^ y2
ans += cnt[(x1, y1)]
cnt[(x2, y2)] += 1
return ans
// Accepted solution for LeetCode #2857: Count Pairs of Points With Distance k
// Rust example auto-generated from java reference.
// Replace the signature and local types with the exact LeetCode harness for this problem.
impl Solution {
pub fn rust_example() {
// Port the logic from the reference block below.
}
}
// Reference (java):
// // Accepted solution for LeetCode #2857: Count Pairs of Points With Distance k
// class Solution {
// public int countPairs(List<List<Integer>> coordinates, int k) {
// Map<List<Integer>, Integer> cnt = new HashMap<>();
// int ans = 0;
// for (var c : coordinates) {
// int x2 = c.get(0), y2 = c.get(1);
// for (int a = 0; a <= k; ++a) {
// int b = k - a;
// int x1 = a ^ x2, y1 = b ^ y2;
// ans += cnt.getOrDefault(List.of(x1, y1), 0);
// }
// cnt.merge(c, 1, Integer::sum);
// }
// return ans;
// }
// }
// Accepted solution for LeetCode #2857: Count Pairs of Points With Distance k
function countPairs(coordinates: number[][], k: number): number {
const cnt: Map<number, number> = new Map();
const f = (x: number, y: number): number => x * 1000000 + y;
let ans = 0;
for (const [x2, y2] of coordinates) {
for (let a = 0; a <= k; ++a) {
const b = k - a;
const [x1, y1] = [a ^ x2, b ^ y2];
ans += cnt.get(f(x1, y1)) ?? 0;
}
cnt.set(f(x2, y2), (cnt.get(f(x2, y2)) ?? 0) + 1);
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Sort the array in O(n log n), then scan for the missing or unique element by comparing adjacent pairs. Sorting requires O(n) auxiliary space (or O(1) with in-place sort but O(n log n) time remains). The sort step dominates.
Bitwise operations (AND, OR, XOR, shifts) are O(1) per operation on fixed-width integers. A single pass through the input with bit operations gives O(n) time. The key insight: XOR of a number with itself is 0, which eliminates duplicates without extra space.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.