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
.
Return a list of all k-distant indices sorted in increasing order.
Example 1:
Input: nums = [3,4,9,1,3,9,5], key = 9, k = 1 Output: [1,2,3,4,5,6] Explanation: Here,nums[2] == key
andnums[5] == key. - For index 0, |0 - 2| > k and |0 - 5| > k, so there is no j
where|0 - j| <= k
andnums[j] == key. Thus, 0 is not a k-distant index. - For index 1, |1 - 2| <= k and nums[2] == key, so 1 is a k-distant index. - For index 2, |2 - 2| <= k and nums[2] == key, so 2 is a k-distant index. - For index 3, |3 - 2| <= k and nums[2] == key, so 3 is a k-distant index. - For index 4, |4 - 5| <= k and nums[5] == key, so 4 is a k-distant index. - For index 5, |5 - 5| <= k and nums[5] == key, so 5 is a k-distant index. - For index 6, |6 - 5| <= k and nums[5] == key, so 6 is a k-distant index.
Thus, we return [1,2,3,4,5,6] which is sorted in increasing order.
Example 2:
Input: nums = [2,2,2,2,2], key = 2, k = 2 Output: [0,1,2,3,4] 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. Hence, we return [0,1,2,3,4].
Constraints:
1 <= nums.length <= 1000
1 <= nums[i] <= 1000
key
is an integer from the array nums
.1 <= k <= nums.length
This problem asks to find all indices in an array that are within a distance k
from any index containing the value key
. We'll explore three approaches with varying time complexities:
This approach directly implements the problem's definition. For each index i
, we check every other index j
to see if |i - j| <= k
and nums[j] == key
. If such a j
exists, i
is added to the result.
Time Complexity: O(n^2) because of the nested loops iterating through all pairs of indices. Space Complexity: O(n) in the worst case to store the result list (all indices could be k-distant).
Code Examples:
def findKDistantIndices(nums, key, k):
result = []
n = len(nums)
for i in range(n):
found = False
for j in range(n):
if abs(i - j) <= k and nums[j] == key:
found = True
break
if found:
result.append(i)
return result
import java.util.ArrayList;
import java.util.List;
class Solution {
public List<Integer> findKDistantIndices(int[] nums, int key, int k) {
List<Integer> result = new ArrayList<>();
int n = nums.length;
for (int i = 0; i < n; i++) {
boolean found = false;
for (int j = 0; j < n; j++) {
if (Math.abs(i - j) <= k && nums[j] == key) {
found = true;
break;
}
}
if (found) {
result.add(i);
}
}
return result;
}
}
#include <vector>
#include <cmath>
std::vector<int> findKDistantIndices(const std::vector<int>& nums, int key, int k) {
std::vector<int> result;
int n = nums.size();
for (int i = 0; i < n; ++i) {
bool found = false;
for (int j = 0; j < n; ++j) {
if (std::abs(i - j) <= k && nums[j] == key) {
found = true;
break;
}
}
if (found) {
result.push_back(i);
}
}
return result;
}
Other languages (JavaScript, Go, etc.) would follow a similar structure.
This approach first finds all indices where nums[i] == key
. Then, for each index i
, it performs a binary search on this preprocessed list to check if there's an index within the k
distance.
Time Complexity: O(n log n) due to the binary search operation repeated for each index. The preprocessing step is O(n). Space Complexity: O(n) to store the preprocessed list of indices.
Code Examples (Illustrative Python):
import bisect
def findKDistantIndices(nums, key, k):
key_indices = [i for i, num in enumerate(nums) if num == key]
result = []
for i in range(len(nums)):
left = bisect.bisect_left(key_indices, i - k)
right = bisect.bisect_right(key_indices, i + k)
if left < right: # Found a key index within k distance
result.append(i)
return result
This is the most efficient approach. We use two pointers: i
iterates through all indices, and j
tracks the nearest index containing key
. We only need to move j
forward when it's too far from i
or doesn't point to key
.
Time Complexity: O(n) because both pointers traverse the array at most once. Space Complexity: O(n) to store the result list.
Code Examples (Illustrative Python):
def findKDistantIndices(nums, key, k):
result = []
j = 0
for i in range(len(nums)):
while j < len(nums) and (j < i - k or nums[j] != key):
j += 1
if j < len(nums) and abs(i - j) <= k:
result.append(i)
return result
The Java, C++, and other language implementations would be very similar in structure to the Python example provided here. The core idea of using a single pass with two pointers remains the same. Remember to handle edge cases appropriately (e.g., when j
goes beyond the array bounds).