Minimum Operations to Equalize Array by Selective Increments
Arrays & HashingAuto-graded · 22 test casesLast updated
Tests insight that incrementing all-but-one is equivalent to decrementing one, leading to a sum-based formula instead of simulation.
You have an integer array. In a single operation, you select exactly one index and increment every element except the one at that index by 1. Determine the minimum number of operations required to make all array elements equal.
Rules
- One operation increments all elements except a single chosen element.
- Different operations may skip different elements.
- If all elements are already equal, the answer is 0.
Constraints
1 <= len(nums) <= 10^5-10^9 <= nums[i] <= 10^9- The result may exceed 2^31 but remains below 2^53.
Examples
Example 1
Input: min_ops_equalize([1, 3])
Output: 2
Example 2
Input: min_ops_equalize([4, 1, 6])
Output: 8
Example 3
Input: min_ops_equalize([-2, 5, -2, 0])
Output: 9
Hints
Hint 1
Think about what happens to the *differences* between elements when you increment all but one.
Hint 2
Incrementing everything except index i has the same effect on relative values as decrementing nums[i] alone.
Hint 3
Since we can only effectively lower values (via the decrement view), what is the best common target?
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
The key insight is recognizing what each operation actually accomplishes. When you increment every element except one, the relative differences between elements change in the same way as if you had decremented that single element by 1.
To see why: if we add 1 to all elements except index i, every pairwise difference nums[j] - nums[i] increases by 1, which is identical to the effect of subtracting 1 from nums[i] alone.
Reframing the problem: we can think of each operation as decrementing a chosen element. Since we can only lower values through this lens, the cheapest achievable common target is the current minimum value in the array. Every element must be brought down to min(nums).
The total operations needed equals the sum of how much each element must decrease:
answer = sum of (nums[i] - min(nums)) for all i
= sum(nums) - len(nums) * min(nums)
Correctness
Let's verify formally. Suppose we perform k operations total, and element i is left unchanged in c_i of those operations.
- Each
c_i >= 0and their sum equalsk - Element
ireceivesk - c_iincrements, reachingnums[i] + k - c_i
For all elements to equal some value v:
nums[i] + k - c_i = v
c_i = nums[i] + k - v
Since c_i >= 0, we need nums[i] + k - v >= 0 for all i, giving us v <= nums[i] + k for all i.
The sum constraint gives: k = sum(c_i) = sum(nums) + n*k - n*v, which simplifies to k = sum(nums) - n*v.
To minimize k, we maximize v. The maximum valid v is min(nums) + k, achieved by setting each c_i = nums[i] - min(nums).
This confirms our formula is optimal.
Complexity
- Time: O(n) for computing sum and minimum in a single pass
- Space: O(1) auxiliary space
No sorting or simulation needed. Direct calculation handles even the maximum case where the answer approaches 2×10^14.
Implementation
def min_ops_equalize(nums: list[int]) -> int:
return sum(nums) - len(nums) * min(nums)