← All problems

K Closest Points to Origin

medium

Given an array of points where points[i] = [x, y], return the k points closest to the origin (0, 0), in any order. Distance is the usual Euclidean distance — comparing squared distances avoids the square root entirely.

Same "always grab the smallest/biggest remaining" shape as Kth Largest Element, scored by distance instead of value.

Example

Input: points = [[1, 3], [-2, 2]], k = 1
Output: [[-2, 2]]        // dist² 8 < dist² 10
Loading...