java.lang.Object
g2101_2200.s2200_find_all_k_distant_indices_in_an_array.Solution

public class Solution extends Object
2200 - Find All K-Distant Indices in an Array.<p>Easy</p> <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| <= k</code> and <code>nums[j] == key</code>.</p> <p>Return <em>a list of all k-distant indices sorted in <strong>increasing order</strong></em>.</p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> nums = [3,4,9,1,3,9,5], key = 9, k = 1</p> <p><strong>Output:</strong> [1,2,3,4,5,6]</p> <p><strong>Explanation:</strong> Here, nums[2] == key and nums[5] == key.</p> <ul> <li> <p>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.</p> </li> <li> <p>For index 1, |1 - 2| <= k and nums[2] == key, so 1 is a k-distant index.</p> </li> <li> <p>For index 2, |2 - 2| <= k and nums[2] == key, so 2 is a k-distant index.</p> </li> <li> <p>For index 3, |3 - 2| <= k and nums[2] == key, so 3 is a k-distant index.</p> </li> <li> <p>For index 4, |4 - 5| <= k and nums[5] == key, so 4 is a k-distant index.</p> </li> <li> <p>For index 5, |5 - 5| <= k and nums[5] == key, so 5 is a k-distant index.</p> </li> <li> <p>For index 6, |6 - 5| <= k and nums[5] == key, so 6 is a k-distant index.</p> </li> </ul> <p>Thus, we return [1,2,3,4,5,6] which is sorted in increasing order.</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> nums = [2,2,2,2,2], key = 2, k = 2</p> <p><strong>Output:</strong> [0,1,2,3,4]</p> <p><strong>Explanation:</strong> 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.</p> <p>Hence, we return [0,1,2,3,4].</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 <= nums.length <= 1000</code></li> <li><code>1 <= nums[i] <= 1000</code></li> <li><code>key</code> is an integer from the array <code>nums</code>.</li> <li><code>1 <= k <= nums.length</code></li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • findKDistantIndices

      public List<Integer> findKDistantIndices(int[] nums, int key, int k)