973. K Closest Points To Origin

3 min read

The problem 973. K Closest Points to Origin asks you to return the k points from a given list that are closest to the origin (0, 0). The answer can be returned in any order, which is an important detail because it means you do not need to sort the final result unless you want to. You are given an array of points where each point is represented as [x, y], along with an integer k. What matters is that the returned set contains exactly the k points with the smallest distances from the origin.

This problem is popular in coding interviews because it looks simple but gives you several valid solution paths. You can sort every point by distance, maintain a heap of the closest points, or use a selection algorithm such as Quickselect. Each approach teaches something different about time complexity, space complexity, and practical trade-offs. Understanding those trade-offs is more valuable than memorizing one solution Not complicated — just consistent..

Understanding the Problem

For a point [x, y], the Euclidean distance from the origin is:

[ \sqrt{x^2 + y^2} ]

On the flip side, in this problem you do not need to compute the square root. Comparing squared distances gives the same ordering as comparing actual distances because the square root function is monotonically increasing for non-negative numbers. That means:

[ x^2 + y^2 ]

is enough to compare two points. This is a common optimization in geometry problems and avoids unnecessary floating-point operations. It also reduces the risk of precision errors.

Here's one way to look at it: consider points = [[1, 3], [-2, 2]] and k = 1 Worth keeping that in mind..

  • Distance squared for [1, 3] is 1^2 + 3^2 = 10.
  • Distance squared for [-2, 2] is (-2)^2 + 2^2 = 8.

Since 8 < 10, the point [-2, 2] is closer to the origin. The correct output is [[-2, 2]] No workaround needed..

For another example, consider points = [[3, 3], [5, -1], [-2, 4]] and k = 2.

  • [3, 3] gives 18.
  • [5, -1] gives 26.
  • [-2, 4] gives 20.

The two smallest squared distances are 18 and 20, so the answer is [[3, 3], [-2, 4]]. The order does not matter.

Why Squared Distance Works

The distance formula includes a square root, but the square root does not change which value is smaller. If a < b, then sqrt(a) < sqrt(b) for non-negative a and b. Since squared distances are always non-negative, comparing x^2 + y^2 is mathematically equivalent to comparing the true Euclidean distances.

Easier said than done, but still worth knowing.

This optimization matters in practice. Square roots are slower than integer multiplication and addition, and floating-point comparisons can introduce tiny errors. Using squared distances keeps the solution exact and efficient.

Approach 1: Sort All Points

The most straightforward solution is to compute the squared distance for every point, sort the points by that distance, and return the first k points The details matter here. Simple as that..

Steps

  1. Create a list of tuples containing (distance, x, y) for each point.
  2. Sort the list by distance.
  3. Return the first k points.

Python Example

def kClosest(points, k):
    points.sort(key=lambda p: p[0] * p[0] + p[1] * p[1])
    return points[:k]

Complexity

  • Time complexity: O(n log n) because sorting dominates the runtime.
  • Space complexity: O(1) or
Just Shared

New and Noteworthy

Worth the Next Click

What Others Read After This

Thank you for reading about 973. K Closest Points To Origin. We hope the information has been useful. Feel free to contact us if you have any questions. See you next time — don't forget to bookmark!
⌂ Back to Home