# Find K Closest Points to the Origin

Given a list of points on the 2-D plane and an integer K. The task is to find K closest points to the origin and print them.

**Note**: The distance between two points on a plane is the Euclidean distance.

**Examples:**

Input :point = [[3, 3], [5, -1], [-2, 4]], K = 2Output :[[3, 3], [-2, 4]] Square of Distance of origin from this point is (3, 3) = 18 (5, -1) = 26 (-2, 4) = 20 So rhe closest two points are [3, 3], [-2, 4].Input :point = [[1, 3], [-2, 2]], K = 1Output :[[-2, 2]] Square of Distance of origin from this point is (1, 3) = 10 (-2, 2) = 8 So the closest point to origin is (-2, 2)

**Approach :** The idea is to calculate the euclidean distance from the origin for every given point and sort the array according to the euclidean distance found. Print the first k closest points from the list.

**Algorithm :**

Consider two points with coordinates as (x1, y1) and (x2, y2) respectively. The **euclidean distance **between these two points will be:

√{(x2-x1)^{2}+ (y2-y1)^{2}}

- Sort the points by distance using Euclidean distance formula.
- Select first K points form the list
- Print the points obtained in any order.

Below is the implementation of the above approach:

`# Python program for implementation of ` `# above approach ` ` ` `# Function to return required answer ` `def` `pClosest(points, K): ` ` ` ` ` `points.sort(key ` `=` `lambda` `K: K[` `0` `]` `*` `*` `2` `+` `K[` `1` `]` `*` `*` `2` `) ` ` ` ` ` `return` `points[:K] ` ` ` `# Driver program ` `points ` `=` `[[` `3` `, ` `3` `], [` `5` `, ` `-` `1` `], [` `-` `2` `, ` `4` `]] ` ` ` `K ` `=` `2` ` ` `print` `(pClosest(points, K)) ` |

*chevron_right*

*filter_none*

**Output:**

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

**Complexity Analysis:**

**Time Complexity:**O(n log n).

Time complexity to find the distance from origin for every point is O(n) and to sort the array is O(n log n)**Space Complexity:**O(1).

As no extra space is required.

GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details

## Recommended Posts:

- Find the K closest points to origin using Priority Queue
- Find the maximum possible distance from origin using given points
- Closest Pair of Points | O(nlogn) Implementation
- Closest Pair of Points using Divide and Conquer algorithm
- Find the multiple of x which is closest to a^b
- Find closest integer with the same weight
- Python | Find the closest Key in dictionary
- Find the number closest to n and divisible by m
- Python | Find closest number to k in given list
- Find multiple of x closest to or a ^ b (a raised to power b)
- Find a triplet in an array whose sum is closest to a given number
- Find number from given list for which value of the function is closest to A
- Find the closest and smaller tidy number
- Python Program for Find the closest pair from two sorted arrays
- Find Corners of Rectangle using mid points

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.