You are given an array `points` where `points[i] = [xi, yi]` is a point on the 2D plane, along with an integer `k`.
Return the `k` points that are closest to the origin `(0, 0)`, measured by Euclidean distance sqrt(x^2 + y^2).
The answer may be returned in any order, and it is guaranteed to be unique except for that ordering.
Example 1
Input: points = [[1,3],[-2,2]], k = 1
Output: [[-2,2]]
The distance of [1,3] is sqrt(10) and of [-2,2] is sqrt(8). Since sqrt(8) < sqrt(10), the single closest point is [-2,2].
Example 2
Input: points = [[3,3],[5,-1],[-2,4]], k = 2
Output: [[3,3],[-2,4]]
The squared distances are 18, 26 and 20; the two smallest belong to [3,3] and [-2,4]. Any order of these two is accepted.
Constraints
1 <= k <= points.length <= 10^4-10^4 <= xi, yi <= 10^4See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.
FDE Coach is a cohort-based program in frontend, backend, AWS, and AI where you build real products and get referred to 200+ hiring partners. The free live workshop is the fastest way to see how we teach.
750+ engineers trained · frontend, backend, AWS & AI