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
isuch that1 <= i < nandnums[i] > 0. - Decrease
nums[i]by1. - Increase
nums[i - 1]by1.
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
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
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code