Minimize Maximum of Array - Problem

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.

Input & Output

Example 1 — Basic Case
$ Input: nums = [3,7,1,6]
Output: 5
💡 Note: We can move values leftward: 7→6 (move 1 left), then distribute optimally to get maximum of 5
Example 2 — Already Optimal
$ Input: nums = [10,1]
Output: 10
💡 Note: Cannot reduce the maximum since we can only move from right to left, and leftmost is already maximum
Example 3 — Equal Distribution
$ Input: nums = [4,9,7,4]
Output: 6
💡 Note: Optimal redistribution gives us [6,6,6,6] with maximum 6

Constraints

  • n == nums.length
  • 2 ≤ n ≤ 105
  • 0 ≤ nums[i] ≤ 109

Visualization

Tap to expand
Minimize Maximum of ArrayOriginal Array3716max = 7move leftAfter Redistribution5534max = 5Goal: Minimize the maximum value by moving elements leftwardResult: Maximum reduced from 7 to 5
Understanding the Visualization
1
Input Array
Original array with varying values
2
Leftward Operations
Move values from right to left to balance
3
Minimized Maximum
Find the smallest possible maximum value
Key Takeaway
🎯 Key Insight: Binary search on the minimum achievable maximum value, since we can only redistribute leftward
Asked in
Google 25 Meta 18 Amazon 15
32.4K Views
Medium Frequency
~25 min Avg. Time
890 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen