- Related Questions & Answers
- Program to maximize the minimum value after increasing K sublists in Python
- Program to find maximum possible value of smallest group in Python
- Find all possible substrings after deleting k characters in Python
- Maximum Possible Product in Array after performing given Operations in C++
- Program to find maximum difference of adjacent values after deleting k numbers in python
- Golang Program to find the minimum and maximum number, using binary operations.
- Program to find maximum profit we can make after k Buy and Sell in python
- Program to find minimum operations to make array equal using Python
- Program to find k partitions after truncating sentence using Python
- Program to find longest equivalent sublist after K increments in Python
- Program to find most occurring number after k increments in python
- Program to find minimum operations to make the array increasing using Python
- Program to find minimum number colors remain after merging in Python
- Program to find maximum time to finish K tasks in Python
- Maximum value in an array after m range increment operations in C++

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

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

- while i < size of nums and nums[i] is same as curr, do
- return curr

Let us see the following implementation to get better understanding:

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))

[3, 4, 6, 5], 6

3

Advertisements