Problem · Array
Closest Pair of Points
Learn this problemProblem statement
Given two equal-length integer arrays xCoordinates and yCoordinates, entry i represents the point (xCoordinates[i], yCoordinates[i]). Return the minimum squared Euclidean distance between any two distinct entries.
For points (x1, y1) and (x2, y2), their squared distance is (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2). Two entries may have identical coordinates, in which case their squared distance is 0.
Function
closestPairSquaredDistance(xCoordinates: int[], yCoordinates: int[]) → longExamples
Example 1
xCoordinates = [0,3]yCoordinates = [0,4]return = 25There is one pair, and its squared distance is 3 * 3 + 4 * 4 = 25.
Example 2
xCoordinates = [0,1,2]yCoordinates = [0,1,2]return = 2Each adjacent diagonal pair has squared distance 1 * 1 + 1 * 1 = 2, which is minimal.
Example 3
xCoordinates = [5,5,9]yCoordinates = [-2,-2,9]return = 0The first two entries have identical coordinates, so the minimum squared distance is 0.
Constraints
2 <= xCoordinates.length = yCoordinates.length <= 100000.-10^9 <= xCoordinates[i], yCoordinates[i] <= 10^9.- Different entries may represent the same coordinates.