# Program to maximize the minimum value after increasing K sublists in Python

PythonServer Side ProgrammingProgramming

Suppose we have a list of numbers called nums and two values, size and k. Now suppose there is an operation where we take a contiguous sublist of length size and increment every element by one. We can perform this operation k times, we have to find the largest minimum value possible in nums.

So, if the input is like nums = [2, 5, 2, 2, 7], size = 3, k = 2, then the output will be 3, as we can increase [2, 5, 2] to get [3, 6, 3, 2, 7] and then increment [6, 3, 2] to get [3, 7, 4, 3, 7], minimum is 3

To solve this, we will follow these steps −

• Define a function possible() . This will take target
• events := A list of size N, and fill with 0
• moves := 0, s := 0
• for i in range 0 to N, do
• s := s + events[i]
• delta := target -(A[i] + s)
• if delta > 0, then
• moves := moves + delta
• s := s + delta
• if i + size < N, then
• events[i + size] := events[i + size] - delta
• return true when moves <= K
• From the main method, do the following
• N := size of A
• left := 0, right := 10^10
• while left < right, do
• mid :=(left + right + 1) / 2
• if possible(mid), then
• left := mid
• otherwise,
• right := mid - 1
• return left

Let us see the following implementation to get better understanding −

## Example

Live Demo

class Solution:
def solve(self, A, size, K):
N = len(A)
def possible(target):
events =  * N
moves = s = 0
for i in range(N):
s += events[i]
delta = target - (A[i] + s)
if delta > 0:
moves += delta
s += delta
if i + size < N:
events[i + size] -= delta
return moves <= K
left, right = 0, 10 ** 10
while left < right:
mid = (left + right + 1)//2
if possible(mid):
left = mid
else:
right = mid - 1
return left
ob = Solution()
nums = [2, 5, 2, 2, 7]
size = 3
k = 2
print(ob.solve(nums, size, k))

## Input

[2, 5, 2, 2, 7], 3, 2

## Output

3