Ninja loves doing maths problems. His friend has asked him to construct an array (0-indexed) of positive integers of size ‘N’ such that.
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.
Given the size of the array ‘N’, the maximum sum of the constructed array ‘L’, and the index 'K’, your task is to find the maximum possible value at the ‘K'th’ index of the constructed array.
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.
Output Format :
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.
Note :
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
2
3 7 1
3 5 0
3
2
For the first test case,
An array of positive integers having size 3 and sum less than or equal to 7 with the value of index 1 maximized is [2,3,2]. Hence, the answer is 3 in this case.
For the second test case,
The possible arrays of positive integers having size 3 and sum less than or equal to 5 with the value of index 0 maximized are [2,1,2], [2,2,1] and [2,1,1]. Hence, the answer is 2 in this case.
2
4 5 1
5 20 2
2
5
Check for every value between 1 to L, if an array could be generated by placing that value at the K'th index.
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.
Algorithm:
O(N*L), where N is the number of elements in the array and L is the maximum sum of the constructed array.
We are doing L iterations and in each iteration, it takes O(N) time to find the minimum sum of the optimal array that could be generated. .Hence the overall time complexity is O(N*L).
O(1).
Constant space is being used. Hence, the overall Space Complexity is O(1).