

You are given ‘POINTS’ = [[1, 1], [3, 1]] and ‘K’ = 2. The answer will be 1.

On day 2, the point (2, 1) will have K = 2 variants(V1, V2).
The first line contains an integer 'T' which denotes the number of test cases.
The first line of each test case contains two space-separated integers, ‘N’ and ‘K’, representing the number of unique viruses and integer ‘K’, respectively.
The next ‘N’ lines contain two-space separated integers ‘X’ and ‘Y’ representing the originating point of the i-th virus variant.
For each test case, print a single integer, denoting the minimum number of days for any point to contain at least k of the unique virus variants.
The output of each test case will be printed in a separate line.
1 <= T <= 10
2 <= N <= 100
2 <= K <= N
POINTS[i].length == 2
1 <= POINTS[i][j] <= 10^7
Time limit: 2 sec
You do not need to input or print anything, as it has already been taken care of. Just implement the given function.
Level - Hard
Prerequisites - Binary Search, Vectors, Sweep Line Algorithm.
We will use binary search to optimally find our answer. Set the lower bound as 0 and upper bound as 10000000. Now, check if it is possible in the ‘mid' number of days for any point to have k unique variants. If yes, we will check for a lesser number of days because we want a minimum. If not, we will increase the number of days to let the virus spread to some points.
For any virus V originating at point (X, Y), on a given day, the spread of this virus V on the grid will be formed as diamond-shaped squares with center at (X, Y), and the distance of the center to each corner is the day.
Now, consider the above image below. We can see that if two of these squares intersect that means the intersection point will have 2 unique variants. So, we just need to find if at point k of these squares intersect or not.
If instead of a diamond-shaped square, if it would have been a normal square we can do a 2D sweep line on each square. So, to get a normal square we will rotate the axis of every diamond-shaped square by 45 degrees.
Sweep Line Algorithm: The idea behind this algorithm is to imagine that a line is swept or moved across the plane, stopping at some points. This algorithm can be used to find any two of the given n lines intersect or not.
This algorithm first sorts the end points of the given lines along the x-axis from left to right.
Then it passes a sweep line(a vertical line that is swept across the plane rightwards) and checks for intersections.
We will use this sweep line to count the overlapping virus at each point we track at a given day.
Algorithm: