Find K Closest Points to the Origin
Last Updated :
03 Oct, 2022
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 = 2
Output : [[3, 3], [-2, 4]]
Square of Distance of origin from this point is
(3, 3) = 18
(5, -1) = 26
(-2, 4) = 20
So the closest two points are [3, 3], [-2, 4].
Input : point = [[1, 3], [-2, 2]], K = 1
Output : [[-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 the Euclidean distance formula.
- Select first K points from the list
- Print the points obtained in any order.
Note:
- In multimap we can directly store the value of {(x2-x1)2 + (y2-y1)2} instead of its square root because of the following property : If sqrt(x) < sqrt(y) the x < y
- Because of this, we have reduced the time complexity (Time complexity of the square root of an integer is O(? n) )
Below is the implementation of the above approach:
C++
#include<bits/stdc++.h>
using namespace std;
void pClosest(vector<vector< int >> pts, int k)
{
multimap< int , int > mp;
for ( int i = 0; i < pts.size(); i++)
{
int x = pts[i][0], y = pts[i][1];
mp.insert({(x * x) + (y * y) , i});
}
for ( auto it = mp.begin();
it != mp.end() && k > 0;
it++, k--)
cout << "[" << pts[it->second][0] << ", "
<< pts[it->second][1] << "]" << "\n" ;
}
int main()
{
vector<vector< int >> points = { { 3, 3 },
{ 5, -1 },
{ -2, 4 } };
int K = 2;
pClosest(points, K);
return 0;
}
|
Java
import java.util.*;
class GFG{
static void pClosest( int [][]pts, int k)
{
int n = pts.length;
int [] distance = new int [n];
for ( int i = 0 ; i < n; i++)
{
int x = pts[i][ 0 ], y = pts[i][ 1 ];
distance[i] = (x * x) + (y * y);
}
Arrays.sort(distance);
int distk = distance[k - 1 ];
for ( int i = 0 ; i < n; i++)
{
int x = pts[i][ 0 ], y = pts[i][ 1 ];
int dist = (x * x) + (y * y);
if (dist <= distk)
System.out.println( "[" + x + ", " + y + "]" );
}
}
public static void main (String[] args)
{
int points[][] = { { 3 , 3 },
{ 5 , - 1 },
{ - 2 , 4 } };
int K = 2 ;
pClosest(points, K);
}
}
|
Python3
def pClosest(points, K):
points.sort(key = lambda K: K[ 0 ] * * 2 + K[ 1 ] * * 2 )
return points[:K]
points = [[ 3 , 3 ], [ 5 , - 1 ], [ - 2 , 4 ]]
K = 2
print (pClosest(points, K))
|
C#
using System;
class GFG{
static void pClosest( int [,]pts,
int k)
{
int n = pts.GetLength(0);
int [] distance = new int [n];
for ( int i = 0; i < n; i++)
{
int x = pts[i, 0],
y = pts[i, 1];
distance[i] = (x * x) +
(y * y);
}
Array.Sort(distance);
int distk = distance[k - 1];
for ( int i = 0; i < n; i++)
{
int x = pts[i, 0],
y = pts[i, 1];
int dist = (x * x) +
(y * y);
if (dist <= distk)
Console.WriteLine( "[" + x +
", " + y + "]" );
}
}
public static void Main ( string [] args)
{
int [,]points = {{3, 3},
{5, -1},
{-2, 4}};
int K = 2;
pClosest(points, K);
}
}
|
Javascript
<script>
function pClosest(pts,k)
{
let n = pts.length;
let distance = new Array(n);
for (let i = 0; i < n; i++)
{
let x = pts[i][0], y = pts[i][1];
distance[i] = (x * x) + (y * y);
}
distance.sort( function (a,b){ return a-b;});
let distk = distance[k - 1];
for (let i = 0; i < n; i++)
{
let x = pts[i][0], y = pts[i][1];
let dist = (x * x) + (y * y);
if (dist <= distk)
document.write( "[" + x + ", " + y + "]<br>" );
}
}
let points = [[3, 3], [5, -1], [-2, 4]];
let K = 2;
pClosest(points, K);
</script>
|
Output:
[[3, 3], [-2, 4]]
Complexity Analysis:
- Time Complexity: O(n log n).
Time complexity to find the distance from the origin for every point is O(n) and to sort the array is O(n log n)
- Space Complexity: O(n).
As we are making an array to store distance from the origin for each point.
Share your thoughts in the comments
Please Login to comment...