#1551
Minimum Operations to Make Array Equal
pupil · 425 · lc medium +25 · verified · 82.7% accepted · 1,502 likes · top 94%
Description
An array of length n is defined as arr[i] = 2 * i + 1 for 0 <= i < n. Each operation lets you pick indices x and y, decrement arr[x] by 1 and increment arr[y] by 1. Return the minimum number of operations needed to make all elements equal.
Example 1:
Input: n = 3
Output: 2
Explanation: arr = [1, 3, 5]
First operation choose x = 2 and y = 0, this leads arr to be [2, 3, 4]
In the second operation choose x = 2 and y = 0 again, thus arr = [3, 3, 3].
Example 2:
Input: n = 6
Output: 9
Code
1
2
3