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++
// C++ implementation to find the K // closest points to origin // using Priority Queue #include <bits/stdc++.h> using namespace std; // Comparator class to assign // priority to coordinates 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 true if distance // of point 1 from origin // is greater than distance of // point 2 from origin return (x1 + y1) > (x2 + y2); } }; // Function to find the K closest points void kClosestPoints(int x[], int y[], int n, int k) { // Create a priority queue priority_queue<pair<int, int>, vector<pair<int, int> >, comp> pq; // Pushing all the points // in the queue for (int i = 0; i < n; i++) { pq.push(make_pair(x[i], y[i])); } // Print the first K elements // of the queue for (int i = 0; i < k; i++) { // Store the top of the queue // in a temporary pair pair<int, int> p = pq.top(); // Print the first (x) // and second (y) of pair cout << p.first << " " << p.second << endl; // Remove top element // of priority queue pq.pop(); } } // Driver code int main() { // x coordinate of points int x[] = { 1, -2 }; // y coordinate of points int y[] = { 3, 2 }; int K = 1; int n = sizeof(x) / sizeof(x[0]); kClosestPoints(x, y, n, K); return 0; } |
Java
// Java implementation to find the K // closest points to origin // using Priority Queue import java.util.*; // Point class to store // a point 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{ // Function to find the K closest points static void kClosestPoints(int x[], int y[], int n, int k) { // Create a priority queue PriorityQueue<Pair> pq = new PriorityQueue<>(); // Pushing all the points // in the queue for(int i = 0; i < n; i++) { pq.add(new Pair(x[i], y[i])); } // Print the first K elements // of the queue for(int i = 0; i < k; i++) { // Remove the top of the queue // and store in a temporary pair Pair p = pq.poll(); // Print the first (x) // and second (y) of pair System.out.println(p.first + " " + p.second); } } // Driver code public static void main(String[] args) { // x coordinate of points int x[] = { 1, -2 }; // y coordinate of points int y[] = { 3, 2 }; int K = 1; int n = x.length; kClosestPoints(x, y, n, K); } } // This code is contributed by jrishabh99 |
-2 2
Time Complexity: O(n + K * log(n))
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Find K Closest Points to the Origin
- Find the maximum possible distance from origin using given points
- Priority Queue using Queue and Heapdict module in Python
- Difference between Circular Queue and Priority Queue
- Applications of Priority Queue
- Priority Queue in Python
- Priority Queue | Set 1 (Introduction)
- Priority Queue of Vectors in C++ STL with Examples
- Priority queue of pairs in C++ (Ordered by first)
- Huffman Coding using Priority Queue
- STL Priority Queue for Structure or Class
- Priority Queue using Linked List
- Double ended priority queue
- Implementation of Priority Queue in Javascript
- Does STL priority queue allow duplicate values?
- Why is Binary Heap Preferred over BST for Priority Queue?
- Implementation of Non-Preemptive Shortest Job First using Priority Queue
- Priority Queue in C++ Standard Template Library (STL)
- Priority Queue using doubly linked list
- Priority queue of pairs in C++ with ordering by first and second element
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.
Improved By : jrishabh99

