


The first line of input contains an integer ‘T,’ denoting the number of test cases. The test cases follow.
The first line contains three space-separated integers ‘n’, ‘dayNumber,’ and ‘maxVaccines,’ denoting the number of days for which this vaccination drive will go on, the total number of vaccines available for the campaign, and the day for which the number of vaccines administered needs to be maximized respectively.
For each test case, print an integer denoting the maximum number of vaccines administered on the day ‘dayNumber’.
Print the output of each test case in a separate line.
You are not required to print the expected output; it has already been taken care of. Just implement the function.
1<= T <= 50
1 <= n <= maxVaccines <= 10^9
0 <= dayNumber < n
Time Limit: 1 sec
The idea is to choose a peak value at the ‘dayNumber’ th index. Then we can create the array like a mountain with the peak of the mountain being at the ‘dayNumber’ th index. The sum of the elements of this array must be less than or equal to maxVaccines.If we find that the sum is greater, then we have chosen a high peak value, and if it is less, then it means we have chosen a smaller peak value. So we have to select the peak value such that given conditions are fulfilled. So we can iterate from maxVaacines to 1 and check if this is the peak value for which the array can be created. If we find such a peak, then we will return its value.
We need to find the maximum number, which can be the peak of the array vaccine,s and the sum of the array will not exceed maxVaccines.
Let the peak of the array or vaccines[dayNumber] be x.
Then, we can say the array may look like [... x-2 x-1 x x-1 x-2 …] since abs([vaccine[i+1] - vaccine[i]) <= 1. However, while making the array, we will not decrease after reaching 1 since vaccines[i] > 0.
We can easily find the formula for the total sum of the array vaccines from the peak value and we do not need to create the whole array.
Let's take the left part of the peak as an example. It can have 2 cases:
The same thing goes with the right side.
If the number of elements of the array on the left side of the peak is less than the peak value, we will get situation 2, and if it is more or equal, then we get situation 1. Now we can use the formula according to the situation and find the sum of elements on the left of the peak. Similarly, we can find the sum of the elements to the right of the peak. So we get the sum of all the elements of the array. Now we see if this sum is equal to, less than, or greater than maxVaccines.
If it is equal or less, then this is the required peak value. If it is more, we have to reduce the peak value till we get the desired value.
Now, we need to iterate from maxCapacity till we find an x that satisfies the sum (vaccines) <= maxVaccines. We will stop iterating when we find such a value, and that will be our answer.
The steps are as follows:
In the previous approach, we were iterating from maxVaccines to 1 to find the peak value for which the sum of vaccines array does not exceed maxVaccines. Here to choose the peak value, we are using binary search.
In binary search, we divide the interval in which the value we are searching for lies into two halves.
Here we set start = 1 , end = maxVaccines and mid = start + (end - start) / 2
We assign the mid value to the peak, and using this value, we calculate the total number of vaccines administered.
If the sum of the vaccine array satisfies the given conditions for mid as the peak value, then there is a possibility that a higher value of peak might also fulfill the requirement.
So, we change left to mid + 1
However, if mid does not satisfy the conditions, it is obvious the value of peak has to be smaller, and we set right to mid - 1.
The above process is repeated till the start becomes greater than the end, and the maximum value of the peak that satisfies the given conditions is our answer.
The steps are as follows: