-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontainsCloseNums.js
27 lines (21 loc) · 1 KB
/
containsCloseNums.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// Given an array of integers nums and an integer k,
// determine whether there are two distinct indices i and j in the array where nums[i] = nums[j]
// and the absolute difference between i and j is less than or equal to k.
// Example
// For nums = [0, 1, 2, 3, 5, 2] and k = 3, the output should be containsCloseNums(nums, k) = true.
// There are two 2s in nums, and the absolute difference between their positions is exactly 3.
// For nums = [0, 1, 2, 3, 5, 2] and k = 2, the output should be
// containsCloseNums(nums, k) = false.
// The absolute difference between the positions of the two 2s is 3, which is more than k.
const containsCloseNums = (nums, k) => {
for (let i = 0; i < nums.length; i++) {
for (let j = 0; j < nums.length; j++) {
if (i !== j && nums[i] === nums[j] && Math.abs(i - j) <= k) {
return true
}
}
}
return false
}
console.log(containsCloseNums([0, 1, 2, 3, 5, 2], 3));
console.log(containsCloseNums([0, 1, 2, 3, 5, 2], 2));