K Closest Points to Origin Problem
K Closest Points to Origin Problem — ExecCode Medium DSA Practice
Solve the K Closest Points to Origin problem on ExecCode. Free online medium DSA practice in Heap. Write and run code in Java, C++, Python — no signup required to run.
Problem description
Given an array of 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 (0, 0). The distance between two points is the Euclidean distance.
Examples
Input points = [[1, 3], [-2, 2]], k = 1; Output [[-2, 2]]. Input points = [[3, 3], [5, -1], [-2, 4]], k = 2; Output [[3, 3], [-2, 4]]. Input points = [[1, 3], [-2, 2], [5, 8], [0, 1]], k = 2; Output [[0, 1], [-2, 2]]
Constraints
1 ≤ k ≤ points.length ≤ 10⁴ -10⁴ ≤ xᵢ, yᵢ ≤ 10⁴
Practice K Closest Points to Origin free on ExecCode. Browse DSA problems, topic map, and placement guides.