Skip to content

Commit 5ea26e6

Browse files
authored
Create 2817-minimum-absolute-difference-between-elements-with-constraint.js
1 parent 5880c4d commit 5ea26e6

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* @param {number[]} nums
3+
* @param {number} x
4+
* @return {number}
5+
*/
6+
var minAbsoluteDifference = function(nums, x) {
7+
const {abs, min, max, floor} = Math
8+
9+
let res = Infinity
10+
let n = nums.length
11+
for(let i = 0; i < n; i++) {
12+
if( i + x <= n - 1) {
13+
let j = n - 1 - i - x
14+
// abs_var.push()
15+
res = min(res, abs(nums[i] - nums[i + x]))
16+
let g = 1
17+
while (j > 0) {
18+
// abs_var.push()
19+
res = min(res, abs(nums[i] - nums[i + x + g]))
20+
g += 1
21+
j -= 1
22+
}
23+
}
24+
25+
}
26+
return res
27+
};

0 commit comments

Comments
 (0)