Given an integer array nums
and an integer k
, determine 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
Explanation: nums[0] == nums[3] and abs(0 - 3) == 3, which is <= k
Example 2:
Input: nums = [1,0,1,1], k = 1
Output: true
Explanation: nums[0] == nums[2] and abs(0 - 2) == 2, which is not <= k. However, nums[2] == nums[3] and abs(2-3) == 1 which is <= k
Example 3:
Input: nums = [1,2,3,1,2,3], k = 2
Output: false
Explanation: Although there are duplicate numbers, the absolute difference between their indices is always greater than k = 2.
Constraints:
1 <= nums.length <= 10^5
-10^9 <= nums[i] <= 10^9
0 <= k <= 10^5
Can you implement a function to solve this problem efficiently?
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:
The brute force way to see if a collection of items has nearby duplicates is to simply check every possible pair of items. We compare each item to all other items within a certain distance to see if they are the same.
Here's how the algorithm would work step-by-step:
def contains_duplicate_ii_brute_force(numbers, allowed_distance):
for first_index in range(len(numbers)):
# Iterate through elements after the current one
for second_index in range(first_index + 1, len(numbers)):
# Check for duplicate values
if numbers[first_index] == numbers[second_index]:
# Verify the distance constraint
distance_between_indices = abs(first_index - second_index)
# Return true if within allowed distance
if distance_between_indices <= allowed_distance:
return True
# No duplicates found within the distance
return False
The key is to remember the last time you saw each number. Instead of checking every pair of numbers, this method remembers recent sightings to quickly identify duplicates within a certain distance.
Here's how the algorithm would work step-by-step:
def containsNearbyDuplicate(numbers, allowed_distance):
last_seen_position = {}
for index, number in enumerate(numbers):
# Check if the current number has been seen before.
if number in last_seen_position:
distance = index - last_seen_position[number]
# If the distance is within the allowed range, return True.
if distance <= allowed_distance:
return True
# Update the last seen position of the current number.
last_seen_position[number] = index
return False
Case | How to Handle |
---|---|
Empty input array | Return false immediately, as there are no elements to compare. |
k is zero | The indices i and j must be distinct, so return false if k is zero and no consecutive duplicates exist. |
k is a large value (close to Integer.MAX_VALUE) | The solution should not overflow when calculating the absolute difference between indices. |
Array contains only one element | Return false as we need two distinct indices to form a pair. |
Array contains all identical elements | The solution should correctly identify duplicate indices within the distance k. |
Array with large numbers and k is also large | The solution's performance shouldn't degrade significantly with large inputs. |
No duplicates exist in the array | The solution should correctly return false when no duplicate elements exist within the specified range. |
Array contains negative numbers | The solution should work correctly with negative numbers in the array. |