Skip to content

Commit 5581777

Browse files
authored
Create 3224-minimum-array-changes-to-make-differences-equal.js
1 parent 4f3155c commit 5581777

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* @param {number[]} nums
3+
* @param {number} k
4+
* @return {number}
5+
*/
6+
const minChanges = function(nums, k) {
7+
const n = nums.length
8+
const arr = Array(1e5 + 1).fill(0)
9+
for(let i = 0; i < n /2; i++) {
10+
const a = nums[i], b = nums[n - 1 - i]
11+
const l = Math.min(a, b), r = Math.max(a, b)
12+
const maxDiff = Math.max(l, r, k - l)
13+
arr[0]--
14+
arr[r - l]--
15+
arr[r - l + 1]++
16+
arr[maxDiff + 1]++
17+
}
18+
let res = n, cur = n
19+
for(const e of arr) {
20+
cur += e
21+
res = Math.min(res, cur)
22+
}
23+
return res
24+
};

0 commit comments

Comments
 (0)