1551. Minimum Operations to Make Array Equal

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Solution {
public int minOperations(int n) {
int midIndex, midValue;
if ((n & 1) == 1) {
midIndex = (n - 1) / 2;
midValue = 2 * midIndex + 1;
} else {
midIndex = n / 2;
midValue = n;
}

int operations = 0;
for (int i = 0; i < midIndex; i++) {
operations += midValue - (2 * i + 1);
}
return operations;
}
}
1
2
3
4
5
6
7
8
9
class Solution {
public int minOperations(int n) {
int operations = 0;
for (int num = 1; num < n; num += 2) {
operations += (n - num);
}
return operations;
}
}
1
2
3
4
5
class Solution {
public int minOperations(int n) {
return n * n / 4;
}
}

References

1551. Minimum Operations to Make Array Equal