.
Input: ‘L’ = 8, ‘B’ = 10, ‘N’ = 1, ‘POS’ = {4}, ‘R’ = {5}
Output: -1
In this case,

As shown in the figure above, even if we fix the only street light of the city, the whole city area can’t be covered. Hence the output is ‘-1’.
The first line will contain the integer ‘T’, the number of test cases.
Each test case consists of three lines.
The first line of input contains three integers, ‘L’, ‘B’, and ‘N’ separated by spaces.
The next line contains space-separated ‘N’ non-negative distinct integers, denoting the positions of the street lights in the city.
And the last line contains space-separated ‘N’ non-negative integers, denoting the radius of luminance of street lights in the city.
For each test case, print the minimum number of street lights required to illuminate the whole city with new street lights or print ‘-1’ if it is impossible to do so.
You don't need to print anything. It has already been taken care of. Just implement the given function.
1 <= ‘T’ <= 10
1 <= ‘N’ <= 10^5
‘N’ <= ‘L’ <= 10^5
1 <= ‘B’ <= 10^4
0 <= ‘POS[i]’ <= ‘L’
All ‘POS[i]’ are distinct, i.e. ‘POS[i] != POS[j]’ holds true for all ‘i != j’ such that ‘1’ <= ‘i’ <= ‘N’ and ‘1’ <= ‘j’ <= ‘N’
0 <= ‘R[i]’ <= 10^4
It is guaranteed that sum of ‘N’ over all test cases is <= 10^5
Time Limit: 1 sec
The idea for this approach is based on the properties of a circle.
From the above image, it is clear that if we select some street lights and the points of intersection of the circle formed by its radius and the line ‘Y = B/2’ i.e. points ‘(X1, B/2)’ and ‘(X2, B/2)’ covers the whole line segment ‘(0, B/2)’ to ‘(L, B/2)’, then each part of the city can experience the luminance of the new street lights.
Now to find ‘X1’ and ‘X2’ we can use the equation of the circle, shown in the figure.
Now for ‘Y = B/2’, the equation of the circle will be:
‘(X-P)*(X-P) + (B/2)*(B/2) = R*R’ — (1)
Now here, ‘X2 = P + D/2’
Now if we substitute the value of ‘X’ as ‘X2’ in the equation - 1, and use the above relation we get:
=> ‘(D/2)*(D/2) + (B/2)*(B/2) = R*R’, multiplying by ‘4’ both side.
=> ‘D*D + B*B = 4*R*R’
=> ‘D = SquareRoot(4*R*R - B*B)’. Let’s call ‘D/2’ as ‘V’ then,
=> ‘V = SquareRoot(4*R*R - B*B) / 2’.
=> ‘X1 = P - V’ and ‘X2 = P + V’.
And as we only have our line segment from ‘X=0’ to ‘X=L’, we can do the following modifications to values of ‘X1’ and ‘X2’.
=> ‘X1 = max(0, P - V)’ and ‘X2 = min(L, P + V)’.
Thus we can get points of intersection in this way, now after this, we have to select some subset of street lights to be fixed and check if it covers the whole line segment ‘(0, B/2)’ to ‘(L, B/2)’, if it doesn’t the answer is ‘-1’, else it’s the minimum of all possibilities.
This can be checked using a recursive algorithm in ‘O((2^N)*N)’ time complexity.
Algorithm:
// This function will check if the selected street lights cover the whole city area.
Bool isCovering(L, SELECTED, N)
// This function will try out every subset of street lights and find the minimum required fixing of street lights.
Void getMinimum(i, L, N, RANGE, SELECTED, &ANS)
// The function will return the minimum number of street lights required to be fixed.
Int cityLights(L, B, N, POS, R)
The idea of the above approach is similar to the brute force approach, but instead of trying out every possibility to fix the street lights, we sort the array formed by intersection range and pick greedily the street lights which have a larger range.
The sorted array will be in such a way that the range with the lowest left value comes first. Thus we can iterate on the array from left to right and keep track of the maximum high value up to which we can go with the current left value, initially its value will be ‘0’. Then we replace the current left value with a maximum high and increment the answer by one, and keep iterating until we reach the end of the array.
If at any point we stay on the same point after one iteration, it means there is an empty space in between street lights range i.e. no intersection, and thus we can return ‘-1’ from there.
Algorithm:
// The function will return the minimum number of street lights required to be fixed.
Int cityLights(L, B, N, POS, R)