Facebook Pixel
K Closest Points to Origin
Medium

Given an array of points where points[i] = [xi, yi] represents a point on the X-Y plane, return the k closest points to the origin (0,0). The distance between two points is calculated using the Euclidean distance formula.

Example:

Input: points = [[1,3],[-2,2]], k = 1
Output: [[-2,2]]
Explanation: The distance of (1,3) is √(1² + 3²) = √10, and (-2,2) is √(2² + 2²) = √8.
             Since √8 is smaller, the closest point is (-2,2).

Input: points = [[3,3],[5,-1],[-2,4]], k = 2
Output: [[3,3],[-2,4]]
Explanation: The two closest points to the origin are (3,3) and (-2,4).
Test Cases

Test Cases

Input
3
1 1
2 2
3 3
1
Expected Output
1 1
Step 1
Step 2
Step 3
Step 1: Identify the Pattern
K Closest Points to Origin
Medium

Given an array of points where points[i] = [xi, yi] represents a point on the X-Y plane, return the k closest points to the origin (0,0). The distance between two points is calculated using the Euclidean distance formula.

Example:

Input: points = [[1,3],[-2,2]], k = 1
Output: [[-2,2]]
Explanation: The distance of (1,3) is √(1² + 3²) = √10, and (-2,2) is √(2² + 2²) = √8.
             Since √8 is smaller, the closest point is (-2,2).

Input: points = [[3,3],[5,-1],[-2,4]], k = 2
Output: [[3,3],[-2,4]]
Explanation: The two closest points to the origin are (3,3) and (-2,4).
Test Cases

Test Cases

Input
3
1 1
2 2
3 3
1
Expected Output
1 1
Step 1
Step 2
Step 3
Step 1: Identify the Pattern