VOOZH about

URL: https://www.geeksforgeeks.org/dsa/find-the-maximum-value-of-the-k-th-smallest-usage-value-in-array/

⇱ Find the maximum value of the K-th smallest usage value in Array - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Find the maximum value of the K-th smallest usage value in Array

Last Updated : 23 Jul, 2025

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.

Binary Search:

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.

  • First, initialize the input array and variables M and K, and call to getVal function, to return us the answer.
  • Now, inside the getVal function,
    • sort the array.
    • initialize the search range from 0 to 1e9.
    • for current mid value, check it is valid or not, if it is valid store it as our answer, and change the range to upper half, otherwise if not valid, change the search range to lower half.

Output
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.

Comment
Article Tags: