Loading…
Loading…
Given a list of 2D points and an integer k, return the k points closest to the origin (0, 0), by Euclidean distance.
Input: points (1,3) and (-2,2), k = 1
Output: -2 2
Comparing distances doesn't require the square root — comparing squared distances (x² + y²) gives the identical ordering and avoids floating-point entirely. Sort by squared distance (or use a max-heap of size k for a large input) and take the first k.
Line 1: number of points R
Next R lines: x y for each point
Last line: k
Print the k closest points, one x y pair per line, closest first.
Input (stdin)
Output
Input (stdin)
Output
Sign in to track solved problems and earn XP.