Given an integer array nums and an integer k, return true if there are two distinct indices i and j in the array such that nums[i] == nums[j] and abs(i - j) <= k.
Example 1:
Input: nums = [1,2,3,1], k = 3 Output: true
Example 2:
Input: nums = [1,0,1,1], k = 1 Output: true
Example 3:
Input: nums = [1,2,3,1,2,3], k = 2 Output: false
Constraints:
1 <= nums.length <= 105-109 <= nums[i] <= 1090 <= k <= 105When 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 strategy is to check every single number in the collection against every other number that comes after it. For each pair of numbers, we check if they are the same and also if their positions are close enough to each other.
Here's how the algorithm would work step-by-step:
def contains_nearby_duplicate_brute_force(numbers_list, maximum_distance):
number_of_elements = len(numbers_list)
# Iterate through each number to select it as the first one for comparison.
for first_index in range(number_of_elements):
# Compare the selected number with every subsequent number in the list.
for second_index in range(first_index + 1, number_of_elements):
# If two numbers are identical, we then check if their indices are close enough.
if numbers_list[first_index] == numbers_list[second_index]:
position_difference = second_index - first_index
# If the distance is within the allowed limit, we have found a valid pair.
if position_difference <= maximum_distance:
return True
# If the loops complete without finding a valid pair, no such duplicate exists.
return FalseInstead of checking every possible pair of numbers, we can process the list one number at a time, keeping track of the most recent location of each number we've seen. This allows us to instantly check if an identical number has appeared nearby without re-scanning.
Here's how the algorithm would work step-by-step:
def containsNearbyDuplicate(numbers_list, max_separation): # This notepad stores the most recent index seen for each unique number. last_seen_location_of_number = {} for current_index, current_number in enumerate(numbers_list): # Check if we've seen this number before to evaluate the distance. if current_number in last_seen_location_of_number: previous_index = last_seen_location_of_number[current_number] distance = current_index - previous_index # If the distance is within the allowed separation, we've found a nearby duplicate. if distance <= max_separation: return True # Update the notepad with the current number's most recent location. last_seen_location_of_number[current_number] = current_index return False| Case | How to Handle |
|---|---|
| Empty input array | The solution should return false immediately as no pairs of indices can be formed. |
| Array with a single element | The solution should return false as there are no two distinct indices to compare. |
| The value of k is 0 | The solution must return false, because the indices must be distinct, so their absolute difference can never be zero. |
| The value of k is negative | The solution should return false, as the absolute difference between indices cannot be less than or equal to a negative number. |
| Array contains no duplicates at all | The solution will correctly iterate through the entire array and return false at the end. |
| Duplicates exist, but their index difference is greater than k | The solution correctly identifies the duplicates but returns false because the index distance condition is never met. |
| All elements in the array are identical | The solution should return true as long as k is 1 or greater and the array has at least two elements. |
| Large input array size with a large k | An efficient solution using a hash map or a sliding window with a set ensures linear time complexity, avoiding a timeout. |