Skip to content

Commit d904abc

Browse files
committed
feat: add solution 2200. Find All K-Distant Indices in an Array
1 parent 5d217f6 commit d904abc

File tree

2 files changed

+108
-0
lines changed

2 files changed

+108
-0
lines changed
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
# [2200. Find All K-Distant Indices in an Array](https://leetcode.com/problems/find-all-k-distant-indices-in-an-array)
2+
3+
## Description
4+
5+
<div class="elfjS" data-track-load="description_content"><p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> and two integers <code>key</code> and <code>k</code>. A <strong>k-distant index</strong> is an index <code>i</code> of <code>nums</code> for which there exists at least one index <code>j</code> such that <code>|i - j| &lt;= k</code> and <code>nums[j] == key</code>.</p>
6+
7+
<p>Return <em>a list of all k-distant indices sorted in <strong>increasing order</strong></em>.</p>
8+
9+
<p>&nbsp;</p>
10+
<p><strong class="example">Example 1:</strong></p>
11+
12+
<pre><strong>Input:</strong> nums = [3,4,9,1,3,9,5], key = 9, k = 1
13+
<strong>Output:</strong> [1,2,3,4,5,6]
14+
<strong>Explanation:</strong> Here, <code>nums[2] == key</code> and <code>nums[5] == key.
15+
- For index 0, |0 - 2| &gt; k and |0 - 5| &gt; k, so there is no j</code> where <code>|0 - j| &lt;= k</code> and <code>nums[j] == key. Thus, 0 is not a k-distant index.
16+
- For index 1, |1 - 2| &lt;= k and nums[2] == key, so 1 is a k-distant index.
17+
- For index 2, |2 - 2| &lt;= k and nums[2] == key, so 2 is a k-distant index.
18+
- For index 3, |3 - 2| &lt;= k and nums[2] == key, so 3 is a k-distant index.
19+
- For index 4, |4 - 5| &lt;= k and nums[5] == key, so 4 is a k-distant index.
20+
- For index 5, |5 - 5| &lt;= k and nums[5] == key, so 5 is a k-distant index.
21+
- For index 6, |6 - 5| &lt;= k and nums[5] == key, so 6 is a k-distant index.
22+
</code>Thus, we return [1,2,3,4,5,6] which is sorted in increasing order.
23+
</pre>
24+
25+
<p><strong class="example">Example 2:</strong></p>
26+
27+
<pre><strong>Input:</strong> nums = [2,2,2,2,2], key = 2, k = 2
28+
<strong>Output:</strong> [0,1,2,3,4]
29+
<strong>Explanation:</strong> For all indices i in nums, there exists some index j such that |i - j| &lt;= k and nums[j] == key, so every index is a k-distant index.
30+
Hence, we return [0,1,2,3,4].
31+
</pre>
32+
33+
<p>&nbsp;</p>
34+
<p><strong>Constraints:</strong></p>
35+
36+
<ul>
37+
<li><code>1 &lt;= nums.length &lt;= 1000</code></li>
38+
<li><code>1 &lt;= nums[i] &lt;= 1000</code></li>
39+
<li><code>key</code> is an integer from the array <code>nums</code>.</li>
40+
<li><code>1 &lt;= k &lt;= nums.length</code></li>
41+
</ul>
42+
</div>
43+
44+
<p>&nbsp;</p>
45+
46+
## Solutions
47+
48+
**Solution: `Two Pointers`**
49+
50+
- Time complexity: <em>O(n)</em>
51+
- Space complexity: <em>O(n)</em>
52+
53+
<p>&nbsp;</p>
54+
55+
### **JavaScript**
56+
57+
```js
58+
/**
59+
* @param {number[]} nums
60+
* @param {number} key
61+
* @param {number} k
62+
* @return {number[]}
63+
*/
64+
const findKDistantIndices = function (nums, key, k) {
65+
const n = nums.length;
66+
const result = [];
67+
let right = 0;
68+
69+
for (let index = 0; index < n; index++) {
70+
while (right < n && (nums[right] !== key || right < index - k)) {
71+
right += 1;
72+
}
73+
74+
if (right === n) return result;
75+
76+
if (Math.abs(index - right) <= k) {
77+
result.push(index);
78+
}
79+
}
80+
81+
return result;
82+
};
83+
```
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* @param {number[]} nums
3+
* @param {number} key
4+
* @param {number} k
5+
* @return {number[]}
6+
*/
7+
const findKDistantIndices = function (nums, key, k) {
8+
const n = nums.length;
9+
const result = [];
10+
let right = 0;
11+
12+
for (let index = 0; index < n; index++) {
13+
while (right < n && (nums[right] !== key || right < index - k)) {
14+
right += 1;
15+
}
16+
17+
if (right === n) return result;
18+
19+
if (Math.abs(index - right) <= k) {
20+
result.push(index);
21+
}
22+
}
23+
24+
return result;
25+
};

0 commit comments

Comments
 (0)