- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- 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

- Related Questions & Answers
- Program to find minimum possible integer after at most k adjacent swaps on digits in Python
- Program to find minimum amplitude after deleting K elements in Python
- 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
- Program to find maximum sum by performing at most k negate operations in Python
- Maximum Possible Product in Array after performing given Operations in C++
- Program to find maximize score after n operations in Python
- 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 lexicographically smallest string after applying operations in Python
- 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 minimum operations to reduce X to zero in Python
- Program to find maximum erasure value in Python

Advertisements