![]() |
VOOZH | about |
Given an array arr[] of size N and with integers M, K. You are allowed to perform an operation where you can increase the value of the least element of the array by 1. You are to do this M times. The task is to find the largest possible value for theKth smallest value among arr[] after M operations.
Examples
Input: M=10, K=4, arr={5,5,5,5,5}.
Output: 7
Explanation: After 5 operations, each element of the array become 6, and similarly after the next 5 operations, each element of the array become 7.Input: M=7, K=2, arr={5,9,3,6,4,3};
Output: 5
Explanation: Can use 5 operations to increase the 3 smallest values to 5. Cannot further improve 2-nd smallest with the remaining 2 operations.Input: M=10,K=4, arr={1,2,3,4,5};
Output: 5
Explanation: Can perform operations to fill all values to 5. The 4-th smallest value is thus 5.
One way is to use binary search to find the maximum such answer value that can be achieved by distributing M among K values. If the current mid value is feasible, then the value of the mid is stored as our potential answer, and the search is continued in the upper half of that current search range. Otherwise, the search is continued in the lower half of the search range.
5
Time Complexity: O(NlogN), where N is the size of the 'arr' vector, as it involves iterating through the entire vector once. The time complexity of the 'getVal' function is O(N log N), where N is the size of the 'arr' vector, as it involves sorting the 'arr' vector and performing a binary search on it.
Auxiliary Space: O(N), as it involves storing the 'arr' vector and the 'temp' vector inside the 'check' function.