K Closest Points to Origin

Imagine you're a drone operator standing at the center of a coordinate system (origin at (0, 0)). You have a list of points scattered across the 2D plane, and you need to identify the K closest points to your position.

Given an array points where points[i] = [xi, yi] represents a point on the X-Y plane and an integer k, return the k closest points to the origin.

The distance is calculated using the Euclidean distance formula: √((x₁ - x₂)² + (y₁ - y₂)²). Since we're measuring from the origin (0, 0), this simplifies to √(x² + y²).

Note: You may return the answer in any order. The answer is guaranteed to be unique (except for the order).

Example: If points = [[1,1],[2,2],[3,3]] and k = 2, return [[1,1],[2,2]] since these are the 2 closest points to origin.

Input & Output

example_1.py — Basic Case
$ Input: points = [[1,1],[2,2],[3,3]], k = 2
Output: [[1,1],[2,2]]
💡 Note: The distances from origin are: [1,1]→√2≈1.41, [2,2]→√8≈2.83, [3,3]→√18≈4.24. The 2 closest points are [1,1] and [2,2].
example_2.py — Mixed Quadrants
$ Input: points = [[3,3],[5,-1],[-2,4]], k = 2
Output: [[-2,4],[3,3]]
💡 Note: Distances: [3,3]→√18≈4.24, [5,-1]→√26≈5.10, [-2,4]→√20≈4.47. The 2 closest are [-2,4] and [3,3]. Order doesn't matter in result.
example_3.py — All Points
$ Input: points = [[0,1],[1,0]], k = 2
Output: [[0,1],[1,0]]
💡 Note: Both points have the same distance √1=1 from origin. Since k=2 and we have exactly 2 points, return both. This shows the edge case where k equals the number of points.

Visualization

Tap to expand
Airport (0,0)✈️ Aircraft Ad² = 18✈️ Aircraft Bd² = 32✈️ Aircraft Cd² = 89✈️ Aircraft Dd² = 130Priority Control TowerMax-Heap (K=2)Aircraft B (d²=32)Aircraft A (d²=18)← Farthest of K=2 (Ready to replace)Aircraft C: d²=89 > 32 → RejectAircraft D: d²=130 > 32 → Reject🎯 Control Tower Strategy:1. Keep priority list of K=2 aircraft (max-heap by distance)2. Farthest priority aircraft stays on top for easy replacement3. When new aircraft appears, compare with farthest → replace if closer
Understanding the Visualization
1
Monitor Aircraft Positions
Each aircraft reports its position [x,y]. Calculate distance using d² = x² + y² (skip square root for efficiency).
2
Maintain Priority List
Keep a priority list of K aircraft. Use max-heap so the farthest aircraft in your priority list is always on top.
3
Update Priority Dynamically
When a new aircraft appears, if it's closer than your current farthest priority aircraft, replace the farthest one.
4
Emergency Landing Clearance
Your final priority list contains the K closest aircraft ready for emergency landing clearance.
Key Takeaway
🎯 Key Insight: Max-heap keeps the "worst" of our K best candidates at the top, making it extremely efficient to decide whether to include new candidates or not!

Time & Space Complexity

Time Complexity
⏱️
O(n log k)

We process n points, and for each point we may do a heap operation (insert/replace) that takes O(log k) time since heap size is at most k.

n
2n
Linearithmic
Space Complexity
O(k)

We only store at most k points in the heap at any time, plus O(k) for the result array.

n
2n
Linear Space

Constraints

  • 1 ≤ k ≤ points.length ≤ 104
  • -104 ≤ xi, yi ≤ 104
  • The answer is guaranteed to be unique (except for the order that it is in)
Asked in
Google 45 Amazon 38 Meta 29 Microsoft 22 Apple 18 Uber 15
89.7K Views
High Frequency
~15 min Avg. Time
2.8K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen