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] == keyandnums[5] == key. - For index 0, |0 - 2| > k and |0 - 5| > k, so there is no jwhere|0 - j| <= kandnums[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 <= 10001 <= nums[i] <= 1000key is an integer from the array nums.1 <= k <= nums.lengthWhen you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force method means we check everything. For this problem, we will look at each spot in the list of numbers and see if it's near a 'special' number. If it is, we remember that spot.
Here's how the algorithm would work step-by-step:
def find_all_k_distant_indices(numbers, key, distance):
k_distant_indices = []
for current_index in range(len(numbers)):
for special_index in range(len(numbers)):
# Check if the current element is a key
if numbers[special_index] == key:
# Check if the special index is within the specified distance
if abs(current_index - special_index) <= distance:
# Add the current index to the result if it's not already there.
if current_index not in k_distant_indices:
k_distant_indices.append(current_index)
k_distant_indices.sort()
return k_distant_indicesThe key is to avoid redundant checks. Instead of independently evaluating each position in the list, we progressively build our answer by only considering the important places and their surroundings.
Here's how the algorithm would work step-by-step:
def find_k_distant_indices(numbers, key, distance):
important_indices = []
for index, number in enumerate(numbers):
if number == key:
important_indices.append(index)
result = []
for i in range(len(numbers)):
# Check if the current index is within distance of any important index.
for important_index in important_indices:
if abs(i - important_index) <= distance:
result.append(i)
#Avoid duplicates by breaking after first match.
break
#Removing duplicates while preserving order.
final_result = []
for index in result:
if index not in final_result:
final_result.append(index)
return final_result| Case | How to Handle |
|---|---|
| Null input array | Return an empty list or throw an IllegalArgumentException. |
| Empty input array | Return an empty list as there are no indices to check. |
| k is negative | Treat k as its absolute value or throw an IllegalArgumentException, since distance cannot be negative. |
| k is zero | The condition `abs(i - j) <= k` becomes `i == j`, so only indices equal to their original value should be added to the list. |
| Array with all identical values | The algorithm should correctly identify all indices within the distance k of any index. |
| Large array size and large k value | Ensure the solution's time complexity is efficient, potentially avoiding nested loops for optimal performance. |
| Integer overflow when calculating absolute difference of indices | Use appropriate data types (e.g., long) to prevent overflow during index subtraction when `i` or `j` are close to Integer.MAX_VALUE or Integer.MIN_VALUE. |
| No indices satisfy the condition | The algorithm should return an empty list when no indices meet the k-distant requirement. |