Minimize Maximum of Array

You are given a 0-indexed array nums comprising of n non-negative integers.

In one operation, you must:

  • Choose an integer i such that 1 <= i < n and nums[i] > 0.
  • Decrease nums[i] by 1.
  • Increase nums[i - 1] by 1.

Return the minimum possible value of the maximum integer of nums after performing any number of operations.

Example 1
Inputnums = [3,7,1,6]
Output5
After the shown optimal operations, nums becomes [5, 5, 2, 5] with maximum value 5, and the maximum cannot be less than 5.
Example 2
Inputnums = [10,1]
Output10
It is optimal to leave nums as is, and since 10 is the maximum value, we return 10.

Constraints

  • n == nums.length
  • 2 <= n <= 10^5
  • 0 <= nums[i] <= 10^9

Asked at 4 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate