Skip to main content
Back to problems
Leetcode
Medium
Arrays
Geometry
Heaps
Google
Meta
Amazon
K Closest Points To Origin

Return the kk points nearest to the origin from a list of 2D points.

Acceptance 100%
Problem Statement

Problem

You are given an array of points in a 2D plane, where each point is represented as [x, y], and an integer k.

Your task is to return any k points whose Euclidean distance to the origin (0, 0) is smallest among all points in the array.

The distance of a point [x, y] from the origin is determined by x2+y2x^2 + y^2 (you do not need to compute the square root).

If multiple points are tied for distance, any valid set of k points is acceptable.

Input Format

  • An integer array points, where points[i] = [x_i, y_i]
  • An integer k

Output Format

  • Return an array containing any k points with the smallest distance to the origin

Constraints

  • 1 <= k <= points.length
  • points.length is at least 1
  • Coordinates may be positive, negative, or zero

Hints

  • Comparing squared distances is enough.
  • Think about how to efficiently keep track of the current closest k points while scanning the array.

Input Format

  • points: list of 2D integer coordinates
  • k: number of closest points to return

Output Format

  • An array of exactly k points selected from the input

Constraints

  • 1 <= k <= points.length
  • Each point has two integer coordinates
  • Return any valid answer when ties exist
Examples
Sample cases returned by the problem API.

Example 1

Input

points = [[1,3],[-2,2]], k = 1

Output

[[-2,2]]

Explanation

The squared distances are 10 and 8, so [-2,2] is closer to the origin.

Example 2

Input

points = [[3,3],[5,-1],[-2,4]], k = 2

Output

[[3,3],[-2,4]]

Explanation

The squared distances are 18, 26, and 20. The two closest points are [[3,3],[-2,4]] in any order.

Premium problem context

Unlock deeper context for this problem

Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.