K closest points(Contest)
K closest points easy
Last updated
K closest points easy
Last updated
Problem Statement :
Given a point P in a 2- dimensional plane, a number K, and a list of N points on the plane. You need to find the K closest points to the given Point P. Here, the distance between two points on a plane is the Euclidean distance.
A point in the xy- plane is represented by two numbers, (x, y), where x and y are the coordinates of the x- and y- axes.
Note: The K closest points will be unique. Input User Task: Since this is a functional problem you don't have to worry about the input. You just have to complete the function kNeighbourPoints() which contains following arguments.
points: Arraylist of N points P: point from where you have to find the K closest points K: integer value
Constraints: 1 ≤ T ≤ 100 1 ≤ N ≤ 10^4 1 ≤ K ≤ N -10^5 ≤ P1, P2 ≤ 10^5 Output For each testcase, you need to return the Arraylist of pair of the K closest points. You may return the answer in any order. The answer is guaranteed to be unique Example Sample Input: 2 5 0 0 3 -2 -1 0 0 1 4 -1 0 1 1 4 0 1 2 -2 1 1 2 3 6 9 2
Sample Output: -1 0 0 0 1 1 -2 1 1 2
Explanation:- For test case 1:- Coordinates of given point :- (0, 0). (-2, -1) is at a distance of √5 (0, 0) is at a distance of 0 (1, 4) is at a distance of √17 (-1, 0) is at a distance of 1 (1, 1) is at a distance of √2 We need 3 closest point so we pick (0, 0), (1, 1), (-1, 0).
link: