


We are suffering from the Second wave of Covid-19. The Government is trying to increase its vaccination drives. Ninja wants to help the Government to plan an effective method to help increase vaccination following safety measures. Time is running out. Can you help the nation?
You are given two positive integers: ‘n,’ ‘maxVaccines’ denoting the number of days for which this vaccination drive will go on and the total number of vaccines available for the drive, respectively. You have to find the number of vaccines administered each day. You are also given a number ‘dayNumber,’ and we are interested to know the maximum number of vaccines that can be administered on ‘dayNumber’ th day.
The rules of the vaccination drive :
1. There should be a positive number of vaccines administered each day during the vaccination drive.
2. The absolute difference between the number of vaccines in two consecutive days should not exceed 1.
3. The sum of all the elements of the vaccines array does not exceed maxVaccines, that is, you cannot administer more vaccines than what is provided to you.
4. Vaccines administered on ‘dayNumber’ th day should be maximized.
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.
Output Format:
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.
Note:
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
2
4 3 9
4 2 6
3
2
In the first test case, the number of days for which vaccination drive will go on is 4, and we are required to find the maximum number of vaccines on day 3 under the condition that the total number of vaccines doesn’t exceed 9 during the vaccination drive.
Ninja can have 1 vaccine on day 1, 2 vaccines on day 2, 3 vaccines on day 3, and 3 vaccines on day 4. This satisfies all the conditions.
So we can have a maximum of 3 vaccines on day 3.
In the second test case, the number of days for which vaccination drive will go on is,4 and we are required to find the maximum number of vaccines on day 2 under the condition that the total number of vaccines doesn’t exceed 6 during the vaccination drive.
Ninja can have 1 vaccine on day 1, 2 vaccines on day 2, 2 vaccines on day 3, and 1 vaccine on day 4. This satisfies all the conditions.
So we can have a maximum of 2 vaccines on day 2.
2
10 8 70
20 12 35
10
4
Can you make an array ‘vaccines’ in which ith index represents the number of vaccines needed to be distributed on day ‘i’? Will distributing the peak number of vaccines on day ‘dayNumber’ help?
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:
O(maxVaccines), where maxVaccines is the total number of vaccines available for the drive.
As we check for all values from maxVaccines to 1, there are at most ‘maxVaccines’ number of iterations. Hence the overall complexity is O(maxVaccines).
O(1)
As we are not using any extra space. Hence, the overall space complexity is O(1).