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] <= 109
0 <= k <= 105
When 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:
We want to find if there are two identical items close to each other in a list. The brute force method is like comparing every item with every other item in the list to see if they are the same and close enough.
Here's how the algorithm would work step-by-step:
def contains_duplicate_ii_brute_force(numbers, maximum_distance):
list_length = len(numbers)
for first_index in range(list_length):
# Iterate through each element in the list.
for second_index in range(first_index + 1, list_length):
# Compare each number to all other numbers that follow.
if numbers[first_index] == numbers[second_index]:
# Check to see if the elements are the same
distance = abs(first_index - second_index)
if distance <= maximum_distance:
# Check if the indices are close enough.
return True
return False
We want to quickly determine if there are two identical numbers within a certain distance of each other. The key idea is to remember the recent locations of numbers we've seen, so we don't have to search through the entire list every time.
Here's how the algorithm would work step-by-step:
def containsNearbyDuplicate(numbers, allowed_distance):
last_seen_index = {}
for index, number in enumerate(numbers):
# Check if the number has been seen before.
if number in last_seen_index:
#Check the distance between the current and previous index
distance = index - last_seen_index[number]
if distance <= allowed_distance:
return True
# Store the current index of the number.
last_seen_index[number] = index
return False
Case | How to Handle |
---|---|
Empty or null input array | Return false immediately since no duplicates can exist. |
k is zero | The algorithm should return false unless there are two identical values at the same index (which is impossible) in which case our algorithm should not crash. |
k is negative | The algorithm should treat k as 0 or throw an exception. |
Array with a single element | Return false immediately, as there cannot be two distinct indices. |
Large input array with all same values and large k | Ensure the solution's time complexity is efficient, such as O(n) using a hash map, to avoid timeouts. |
Large k value (k > array length) | The algorithm should function correctly, effectively searching the entire array for duplicates. |
Array with integer overflow values | The algorithm's integer subtraction for index difference should not cause issues if the indices are reasonably sized (within normal integer bounds). |
Array with large range and many duplicates far apart | The sliding window approach should efficiently iterate through and not consume excessive memory with large hashmap. |