2200. Find All K-Distant Indices in an Array Easy
1/**
2 * [2200] Find All K-Distant Indices in an Array
3 *
4 * You are given a 0-indexed integer array nums and two integers key and k. A k-distant index is an index i of nums for which there exists at least one index j such that |i - j| <= k and nums[j] == key.
5 * Return a list of all k-distant indices sorted in increasing order.
6 *
7 * Example 1:
8 *
9 * Input: nums = [3,4,9,1,3,9,5], key = 9, k = 1
10 * Output: [1,2,3,4,5,6]
11 * Explanation: Here, nums[2] == key and nums[5] == key.
12 * - For index 0, |0 - 2| > k and |0 - 5| > k, so there is no j where |0 - j| <= k and nums[j] == key. Thus, 0 is not a k-distant index.
13 * - For index 1, |1 - 2| <= k and nums[2] == key, so 1 is a k-distant index.
14 * - For index 2, |2 - 2| <= k and nums[2] == key, so 2 is a k-distant index.
15 * - For index 3, |3 - 2| <= k and nums[2] == key, so 3 is a k-distant index.
16 * - For index 4, |4 - 5| <= k and nums[5] == key, so 4 is a k-distant index.
17 * - For index 5, |5 - 5| <= k and nums[5] == key, so 5 is a k-distant index.
18 * - For index 6, |6 - 5| <= k and nums[5] == key, so 6 is a k-distant index.
19 * Thus, we return [1,2,3,4,5,6] which is sorted in increasing order.
20 *
21 * Example 2:
22 *
23 * Input: nums = [2,2,2,2,2], key = 2, k = 2
24 * Output: [0,1,2,3,4]
25 * Explanation: For all indices i in nums, there exists some index j such that |i - j| <= k and nums[j] == key, so every index is a k-distant index.
26 * Hence, we return [0,1,2,3,4].
27 *
28 *
29 * Constraints:
30 *
31 * 1 <= nums.length <= 1000
32 * 1 <= nums[i] <= 1000
33 * key is an integer from the array nums.
34 * 1 <= k <= nums.length
35 *
36 */
37pub struct Solution {}
38
39// problem: https://leetcode.com/problems/find-all-k-distant-indices-in-an-array/
40// discuss: https://leetcode.com/problems/find-all-k-distant-indices-in-an-array/discuss/?currentPage=1&orderBy=most_votes&query=
41
42// submission codes start here
43
44impl Solution {
45 pub fn find_k_distant_indices(nums: Vec<i32>, key: i32, k: i32) -> Vec<i32> {
46 vec![]
47 }
48}
49
50// submission codes end
51
52#[cfg(test)]
53mod tests {
54 use super::*;
55
56 #[test]
57 fn test_2200() {
58 }
59}
60
Back
© 2025 bowen.ge All Rights Reserved.