K Closest Points to Origin
Given an array points where points[i] = [x, y] is a point on a plane, and an integer k, return the k points closest to the origin (0, 0).
Distance is the usual straight-line distance, sqrt(x^2 + y^2). Return the points in any order. The tests are built so that the k closest points are unique.
Example 1
Input: points = [[1, 3], [-2, 2]], k = 1 Output: [[-2, 2]]
[1, 3] is sqrt(10) from the origin and [-2, 2] is sqrt(8), so [-2, 2] is closer.
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, so the two closest are [3, 3] and [-2, 4]. [[-2, 4], [3, 3]] is also accepted.
Constraints
1 <= k <= points.length <= 10^4-10^4 <= x, y <= 10^4
Approach
Sorting all the points by distance and taking the first k works in O(n log n). Since k is often much smaller than n, you can do better by tracking only the k closest points seen so far.
First, compare squared distances, x^2 + y^2. Squaring preserves the order, so you skip the square root and its rounding. Then scan the points with a max-heap ordered by that distance. Push each point. If the heap grows to k + 1, pop the top: it's the farthest point in the heap, so it can't be among the k closest. When the scan ends, the heap holds exactly the answer.
Steps
- Create an empty max-heap keyed by squared distance.
- For each point, compute
x * x + y * yand push the point. - If the heap has more than
kpoints, pop the farthest one. - Return the points left in the heap.
import heapq
from typing import List
def k_closest(points: List[List[int]], k: int) -> List[List[int]]:
heap = [] # max-heap of the k closest points so far, stored as (-distance, x, y)
for x, y in points:
dist = x * x + y * y # squared distance keeps the same order and avoids square roots
heapq.heappush(heap, (-dist, x, y))
if len(heap) > k:
heapq.heappop(heap) # drop the farthest of the k + 1 points
return [[x, y] for _, x, y in heap]Complexity
- Time:
O(n log k). Each push and pop works on a heap of at mostk + 1points. - Space:
O(k)for the heap.
Common mistakes
- Using a min-heap of all
npoints, which works but costsO(n)space and doesn't usekto save time. - Keeping a min-heap of size
k. Popping its top throws away the closest point instead of the farthest. - Using
sqrtfor comparisons. It's slower and floating-point rounding can make close points compare in the wrong order.

General Approach (using Max-Heap)
k.P. Push it into the heap. If heap size >k, remove the farthest point.P.import java.util.*;
public class KClosestPoints {
static class Point { int x, y; public Point(int x, int y) { this.x = x; this.y = y; } // Euclidean distance squared (no need to take square root) public int distanceSquared(Point p) { int dx = x - p.x; int dy = y - p.y; return dx * dx + dy * dy; } @Override public String toString() { return "(" + x + ", " + y + ")"; } } public static List<Point> kClosest(Point[] points, Point target, int k) { // Max-heap using comparator on distance PriorityQueue<Point> maxHeap = new PriorityQueue<>((a, b) -> Integer.compare(b.distanceSquared(target), a.distanceSquared(target)) ); for (Point point : points) { maxHeap.add(point); if (maxHeap.size() > k) { maxHeap.poll(); // remove the farthest point } } return new ArrayList<>(maxHeap); } public static void main(String[] args) { Point[] points = { new Point(1, 2), new Point(3, 4), new Point(2, -1), new Point(-1, -2), new Point(5, 2) }; Point target = new Point(0, 0); int k = 3; List<Point> closest = kClosest(points, target, k); System.out.println("K closest points to " + target + ": " + closest); }}