Sorting · Hard
A spatial inspection tool needs the minimum separation between recorded locations. Two different records can have exactly the same coordinates and must then produce zero separation. Large batches make comparing every pair too expensive; no map service or geometry library is available.
Return the minimum squared Euclidean distance between two different input indexes or null if fewer than two records exist.
Input
Call
- Implement solve(input) and return the result. The provided adapter handles JSON input and output.
Input shape
{ points: [integer, integer][] }
Fields
- points contains two-dimensional integer coordinates [x,y] in arbitrary order. Duplicate coordinates may occur at different indexes.
Output
Return value
- Return an integer squared distance: (x1-x2)^2+(y1-y2)^2. Do not take a square root or round.
- Two equal coordinate pairs at different indexes are a valid pair with result zero. Empty and singleton arrays return null.
Constraints
- 0 <= points.length <= 100000; coordinates are integers from -1000000 to 1000000.
- Squared distances and cross products fit safe JSON integers. Do not mutate points or its nested pairs.
Examples
Example 1
{
"points": [
[
0,
0
],
[
3,
4
],
[
4,
4
]
]
}Returns
1Example 2
{
"points": [
[
2,
3
],
[
2,
3
]
]
}Returns
0Required complexity
O(n log(n + 1)) time and O(n) auxiliary space