You are given an array of points of size ‘n’. such that point[i]=[xi, yi] where 'xi' is coordinate and 'yi' is y coordinate of the 'ith' point. You need to find 'k' closest points from the origin (0,0).
The distance between two points ('x1', 'y1') and ('x2', 'y2') is given as (x1-x2)^2 +(y1-y2)^2.

If two points have the same distance then the one with minimum 'x' coordinate will be chosen.
The first line of input contains an integer ‘T’ denoting the number of test cases.
The first line of the test case consists of two integers ‘n’ indicating a number of points and 'k'.
The next line ‘n’ lines consist of 2 space-separated integers 'xi' and 'yi', such that 'xi' is x-coordinate and 'yi' is y-coordinate of the 'ith' point.
Output Format:
Return the list of 'k' points. Return the list in ascending order of distance from the origin. Such that the one with minimum distance will be first. If two points have the same distance then one with a minimum x-coordinate will be first.
Print the output of each test case in a separate line.
1 <= ’T ’<= 10
1 <= ’n’ <= 10^6
1 <= ’k’ <= ’n’
-10^6 <= xi, yi <= 10^6
2
4 3
1 1
-1 1
2 2
-2 2
4 4
1 0
2 0
3 0
4 0
-1 1
1 1
-2 2
1 0
2 0
3 0
4 0
For first case The distance of the points from origin
are :-
(1,1) => (1-0)^2+(1-0)^2=2
(-1,1) => (-1-0)^2 +(1-0)^2=2
(2,2)=>(2-0)^2+(2-0)^2=8
(-2,2)=>(-2-0)^2+(2-0)^2=8
Thus the answer will be in order
(-1,1) -> (1,1)->(-2,2)
The first two points have the same distance but the x coordinate of the second point is less. Hence the second point comes first.
For Second Case
(1,0) => (1-0)^2+(0-0)^2=1
(2,0) => (2-0)^2 +(0-0)^2=4
(3,0)=>(3-0)^2+(0-0)^2=9
(4,0)=>(4-0)^2+(0-0)^2=16
Thus the answer will be in order
(1,0) ->(2,0)->(3,0)->(4,0)
2
4 2
2 5
4 4
5 1
4 2
4 4
4 4
3 3
4 3
1 2
4 2
5 1
1 2
3 3
4 3
4 4
Try to use an approach similar to quick sort in this problem.
This approach is similar to quick sort. Using this approach, we will try to sort the first K element in their respective places.
We have created a function quickSelection(points, l, r, k), where points are the given array, k is the given integer, l and r is the current range in which we are trying to sort the elements.
During partition, we have selected the last element as the pivot element. We will try to find the correct position of the pivot element, and we will return the pivot index.
Algorithm:
O(n^2), where n is the number of points.
The partition index will be the last index in the range of indices, and we are iterating through the whole range to find the partition index, which will take O(n^2) time. Hence the overall time complexity is O(n^2).
O(n), where n is the number of points.
The recursion stack takes O(n) in the worst case. Hence the overall space complexity is O(n).