Skip to content

Commit 7bd8125

Browse files
authored
Create 1551-minimum-operations-to-make-array-equal.js
1 parent 44f7b17 commit 7bd8125

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* @param {number} n
3+
* @return {number}
4+
*/
5+
var minOperations = function(n) {
6+
let l = 1, r = 2 * (n - 1) + 1
7+
8+
// [1, 3]
9+
// [1, 3, 5, 7]
10+
11+
// [1]
12+
// [1, 3, 5]
13+
// [1, 3, 5, 7, 9]
14+
const target = l + (r - l) / 2
15+
let res = 0
16+
const num = ~~((n + 1) / 2)
17+
for(let i = 1; i <= num; i++) {
18+
res += target - (2 * (i -1) + 1)
19+
}
20+
21+
return res
22+
};

0 commit comments

Comments
 (0)