Problem · Array

Closest Pair of Points

Learn this problem
HardCitadel logoCitadelFULLTIMEPHONE SCREEN

Problem 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[]) → long

Examples

Example 1

xCoordinates = [0,3]yCoordinates = [0,4]return = 25

There is one pair, and its squared distance is 3 * 3 + 4 * 4 = 25.

Example 2

xCoordinates = [0,1,2]yCoordinates = [0,1,2]return = 2

Each 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 = 0

The 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.

More Citadel problems

drafts saved locally
public long closestPairSquaredDistance(int[] xCoordinates, int[] yCoordinates) {
    // Write your code here.
}
xCoordinates[0,3]
yCoordinates[0,4]
expected25
checking account