Leetcode 2857: Count Pairs of Points With Distance k

grid47
grid47
Exploring patterns and algorithms
Jan 26, 2024 5 min read

Given a list of 2D points and an integer k, find the number of pairs of points whose distance (calculated as XOR sum of their coordinates) equals k.
Problem
Approach
Steps
Complexity
Input: The input consists of a list of 2D points and an integer k.
Example: coordinates = [[1, 2], [4, 2], [1, 3], [5, 2]], k = 5
Constraints:
• 2 <= coordinates.length <= 50000
• 0 <= xi, yi <= 10^6
• 0 <= k <= 100
Output: Return the number of pairs (i, j) where i < j and the distance between points i and j equals k.
Example: Output: 2
Constraints:
• The output is an integer representing the number of valid pairs.
Goal: Calculate the number of valid pairs of points whose XOR-based distance equals k.
Steps:
• Initialize a hash map to store the count of points encountered so far.
• Iterate through the points and for each point, calculate the potential pairings that result in the required distance k.
• For each valid pair, increment the result count.
• Return the final result.
Goal: Constraints for the input array and k.
Steps:
• The array contains between 2 and 50,000 points.
• Each point's coordinates are between 0 and 1,000,000.
• The distance value k is between 0 and 100.
Assumptions:
• The input coordinates are non-negative integers.
• The value of k is not negative.
Input: coordinates = [[1, 2], [4, 2], [1, 3], [5, 2]], k = 5
Explanation: Valid pairs (0, 1) and (2, 3) both have a distance of 5.

Input: coordinates = [[1, 3], [1, 3], [1, 3], [1, 3], [1, 3]], k = 0
Explanation: Any two points have a distance of 0, so there are 10 ways to select two points from the list.

Link to LeetCode Lab


LeetCode Solutions Library / DSA Sheets / Course Catalog
comments powered by Disqus