Find the K closest points to origin using Priority Queue
Last Updated :
19 Sep, 2022
Given a list of n points on 2D plane, the task is to find the K (k < n) closest points to the origin O(0, 0).
Note: The distance between a point P(x, y) and O(0, 0) using the standard Euclidean Distance.
Examples:
Input: [(1, 0), (2, 1), (3, 6), (-5, 2), (1, -4)], K = 3
Output: [(1, 0), (2, 1), (1, -4)]
Explanation:
Square of Distances of points from origin are
(1, 0) : 1
(2, 1) : 5
(3, 6) : 45
(-5, 2) : 29
(1, -4) : 17
Hence for K = 3, the closest 3 points are (1, 0), (2, 1) & (1, -4).
Input: [(1, 3), (-2, 2)], K = 1
Output: [(-2, 2)]
Explanation:
Square of Distances of points from origin are
(1, 3) : 10
(-2, 2) : 8
Hence for K = 1, the closest point is (-2, 2).
Approach using sorting based on distance: This approach is explained in this article.
Approach using Priority Queue for comparison: To solve the problem mentioned above, the main idea is to store the coordinates of the point in a priority queue of pairs, according to the distance of the point from the origin. For assigning the maximum priority to the least distant point from the origin, we use the Comparator class in Priority Queue. Then print the first K elements of the priority queue.
Below is the implementation of above approach:
C++
#include <bits/stdc++.h>
using namespace std;
class comp {
public :
bool operator()(pair< int , int > a,
pair< int , int > b)
{
int x1 = a.first * a.first;
int y1 = a.second * a.second;
int x2 = b.first * b.first;
int y2 = b.second * b.second;
return (x1 + y1) > (x2 + y2);
}
};
void kClosestPoints( int x[], int y[],
int n, int k)
{
priority_queue<pair< int , int >,
vector<pair< int , int > >,
comp>
pq;
for ( int i = 0; i < n; i++) {
pq.push(make_pair(x[i], y[i]));
}
for ( int i = 0; i < k; i++) {
pair< int , int > p = pq.top();
cout << p.first << " "
<< p.second << endl;
pq.pop();
}
}
int main()
{
int x[] = { 1, -2 };
int y[] = { 3, 2 };
int K = 1;
int n = sizeof (x) / sizeof (x[0]);
kClosestPoints(x, y, n, K);
return 0;
}
|
Java
import java.util.*;
class Pair implements Comparable<Pair>
{
int first, second;
Pair( int a, int b)
{
first = a;
second = b;
}
public int compareTo(Pair o)
{
int x1 = first * first;
int y1 = second * second;
int x2 = o.first * o.first;
int y2 = o.second * o.second;
return (x1 + y1) - (x2 + y2);
}
}
class GFG{
static void kClosestPoints( int x[], int y[],
int n, int k)
{
PriorityQueue<Pair> pq = new PriorityQueue<>();
for ( int i = 0 ; i < n; i++)
{
pq.add( new Pair(x[i], y[i]));
}
for ( int i = 0 ; i < k; i++)
{
Pair p = pq.poll();
System.out.println(p.first +
" " + p.second);
}
}
public static void main(String[] args)
{
int x[] = { 1 , - 2 };
int y[] = { 3 , 2 };
int K = 1 ;
int n = x.length;
kClosestPoints(x, y, n, K);
}
}
|
Python3
import heapq as hq
def kClosestPoints(x, y, n, k):
pq = []
for i in range (n):
hq.heappush(pq, (x[i] * x[i] + y[i] * y[i],x[i],y[i]))
for i in range (k) :
p = hq.heappop(pq)
print ( "{} {}" . format (p[ 1 ],p[ 2 ]))
if __name__ = = '__main__' :
x = [ 1 , - 2 ]
y = [ 3 , 2 ]
K = 1
n = len (x)
kClosestPoints(x, y, n, K)
|
C#
using System;
using System.Collections.Generic;
class Pair : IComparable<Pair> {
public int first, second;
public Pair( int a, int b)
{
first = a;
second = b;
}
public int CompareTo(Pair o)
{
int x1 = first * first;
int y1 = second * second;
int x2 = o.first * o.first;
int y2 = o.second * o.second;
return (x1 + y1) - (x2 + y2);
}
}
class GFG {
static void kClosestPoints( int [] x, int [] y, int n,
int k)
{
List<Pair> pq = new List<Pair>();
for ( int i = 0; i < n; i++) {
pq.Add( new Pair(x[i], y[i]));
}
pq.Sort();
for ( int i = 0; i < k; i++) {
Pair p = pq[0];
pq.RemoveAt(0);
Console.WriteLine(p.first + " " + p.second);
}
}
public static void Main( string [] args)
{
int [] x = { 1, -2 };
int [] y = { 3, 2 };
int K = 1;
int n = x.Length;
kClosestPoints(x, y, n, K);
}
}
|
Javascript
<script>
function kClosestPoints(x, y, n, k)
{
var pq = [];
for ( var i = 0; i < n; i++) {
pq.push([x[i], y[i]]);
}
for ( var i = 0; i < k; i++) {
var p = pq[pq.length-1];
document.write( p[0] + " "
+ p[1] + "<br>" );
pq.pop();
}
}
var x = [1, -2];
var y = [3, 2];
var K = 1;
var n = x.length;
kClosestPoints(x, y, n, K);
</script>
|
Time Complexity: O(N + K * log(N))
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...