# Program to find minimum possible maximum value after k operations in python

Suppose we have a list of numbers called nums and another value k. Now let us consider an operation where we can subtract 1 from any element in the list. We can perform this operation k times. We have to find the minimum possible maximum value in the list after k such operations.

So, if the input is like nums = [3, 4, 6, 5] k = 6, then the output will be 3, as we can decrease 4 once, 6 thrice and 5 twice to get [3,3,3,3].

To solve this, we will follow these steps:

• sort numbers in reverse order
• i := 0
• curr := nums[0]
• while k > 0, do
• while i < size of nums and nums[i] is same as curr, do
• i := i + 1
• if k >= i, then
• k := k - i
• curr := curr - 1
• otherwise,
• return curr
• return curr

Let us see the following implementation to get better understanding:

## Example

Live Demo

class Solution:
def solve(self, nums, k):
nums.sort(reverse=True)
i = 0
curr = nums[0]
while k > 0:
while i < len(nums) and nums[i] == curr:
i += 1
if k >= i:
k -= i
curr -= 1
else:
return curr
return curr

ob = Solution()
nums = [3, 4, 6, 5]
k = 6
print(ob.solve(nums, k))

## Input

[3, 4, 6, 5], 6

## Output

3

Updated on: 26-Nov-2020

776 Views