1551. Minimum Operations to Make Array Equal Medium
1/**
2 * [1551] Minimum Operations to Make Array Equal
3 *
4 * You have an array arr of length n where arr[i] = (2 * i) + 1 for all valid values of i (i.e., 0 <= i < n).
5 * In one operation, you can select two indices x and y where 0 <= x, y < n and subtract 1 from arr[x] and add 1 to arr[y] (i.e., perform arr[x] -=1 and arr[y] += 1). The goal is to make all the elements of the array equal. It is guaranteed that all the elements of the array can be made equal using some operations.
6 * Given an integer n, the length of the array, return the minimum number of operations needed to make all the elements of arr equal.
7 *
8 * Example 1:
9 *
10 * Input: n = 3
11 * Output: 2
12 * Explanation: arr = [1, 3, 5]
13 * First operation choose x = 2 and y = 0, this leads arr to be [2, 3, 4]
14 * In the second operation choose x = 2 and y = 0 again, thus arr = [3, 3, 3].
15 *
16 * Example 2:
17 *
18 * Input: n = 6
19 * Output: 9
20 *
21 *
22 * Constraints:
23 *
24 * 1 <= n <= 10^4
25 *
26 */
27pub struct Solution {}
28
29// problem: https://leetcode.com/problems/minimum-operations-to-make-array-equal/
30// discuss: https://leetcode.com/problems/minimum-operations-to-make-array-equal/discuss/?currentPage=1&orderBy=most_votes&query=
31
32// submission codes start here
33
34impl Solution {
35 pub fn min_operations(n: i32) -> i32 {
36 0
37 }
38}
39
40// submission codes end
41
42#[cfg(test)]
43mod tests {
44 use super::*;
45
46 #[test]
47 fn test_1551() {
48 }
49}
50
Back
© 2025 bowen.ge All Rights Reserved.