
1. The sum of all elements of the array should not be more than ‘L’.
2. The absolute difference between any adjacent pair of indices in the array should not be more than 1.
3. The value at the K'th index of the array should be maximized.
The first line of the input contains an integer, 'T,’ denoting the number of test cases.
The first line of each test case contain three space-separated integers, 'N', 'L', and ‘K’, denoting the number of elements in the array, the maximum sum of the constructed array, and the index whose value needs to be maximized respectively.
For each test case, print a single integer - the maximum possible value at the ‘K'th’ index of the constructed array.
Print the output of each test case in a separate line.
You do not need to print anything. It has already been taken care of. Just implement the given function.
1 <= T <= 10
1 <= N <= 10^5
1 <= L <= 10^9
0 <= K < N
Where 'T' denotes the number of test cases, 'N' denotes the number of elements in the array, 'L' denotes the maximum sum of the constructed array, and 'K’ denotes the index whose value needs to be maximized.
Time limit: 1 sec
Our approach is to try to place every value between 1 to L at the K'th index and check if an array can be constructed or not. We will try to find the array having a minimum sum generated by placing the value at the K'th index and comparing the sum of the array with L.
For a particular value val at K'th index, the array having minimum sum will have the below form:
[..,max(1,val-2), max(1,val-1), val, max(1,val-1), max(1,val-2),..]We want all elements of the array to have the minimum value possible to obtain a minimal sum. To get the minimum sum, we are trying to decrease val by 1 at every step as the difference between any adjacent pair of indices can not exceed 1. Once val is reduced to 1, then all the remaining indexes will be assigned 1 because we are trying to construct an array with positive integers.
The intuition behind the approach is to observe the fact that if the array can be constructed with the value V at the K'th index having a sum less than or equal to L, then the array can be constructed by all values less than V at the K'th index. Similarly, if the array cannot be constructed with the value V at the K'th index having a sum less than or equal to L, then no array can be constructed by all values greater than V at K'th index that satisfies the above conditions.
Therefore, our approach will be to use binary search to solve the problem. At each step of binary search, we will find the sum of the optimal array with the minimum sum that could be generated by placing the value V at the K'th index and compare the obtained value with L. For a particular value val at K'th index, the array having minimum sum will have the below form
[..,max(1,val-2), max(1,val-1),val,max(1,val-1),max(1,val-2),..]The formula we will be using in order to find the sum of the array is the sum of the first n natural numbers, i.e., (n*(n+1))/2 as we are trying to decrease val by 1 at every step till the value reaches the minimum value of the array that is 1 to obtain the optimal array with minimum sum.
We will find the sum of the left and right array to the K’th index separately. To obtain the sum of the left array, we will find the value at the 0’th index of our constructed array, which is equal to val-K. Let the obtained value be start.
Now there will be two cases:
To obtain the sum of the right array to the K’th index, we will find end, which stores the value at the N-1 index, which is equal to val-(N-1-K).