You are given an integer array nums
and two integers indexDiff
and valueDiff
.
Find a pair of indices (i, j)
such that:
i != j
,abs(i - j) <= indexDiff
.abs(nums[i] - nums[j]) <= valueDiff
, andReturn true
if such pair exists or false
otherwise.
Example 1:
Input: nums = [1,2,3,1], indexDiff = 3, valueDiff = 0 Output: true Explanation: We can choose (i, j) = (0, 3). We satisfy the three conditions: i != j --> 0 != 3 abs(i - j) <= indexDiff --> abs(0 - 3) <= 3 abs(nums[i] - nums[j]) <= valueDiff --> abs(1 - 1) <= 0
Example 2:
Input: nums = [1,5,9,1,5,9], indexDiff = 2, valueDiff = 3 Output: false Explanation: After trying all the possible pairs (i, j), we cannot satisfy the three conditions, so we return false.
Constraints:
2 <= nums.length <= 105
-109 <= nums[i] <= 109
1 <= indexDiff <= nums.length
0 <= valueDiff <= 109
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 strategy for determining if almost duplicate numbers exist within a certain range is to simply compare every possible pair of numbers. We'll examine all the numbers and check if any other number within the range is similar to it. This is like meticulously comparing each item in a collection against all the others to find a match that fits our criteria.
Here's how the algorithm would work step-by-step:
def contains_duplicate_iii_brute_force(numbers, index_distance, value_distance):
list_length = len(numbers)
for first_index in range(list_length):
# Outer loop iterates through each element
for second_index in range(first_index + 1, min(list_length, first_index + index_distance + 1)):
# Check neighbors within index_distance
if abs(numbers[first_index] - numbers[second_index]) <= value_distance:
# Value diff within value_distance
return True
return False
The goal is to quickly find if there are two numbers within a certain distance of each other that are also close in value. We avoid checking all pairs by organizing the numbers into groups based on their values, allowing us to only compare numbers within the relevant groups.
Here's how the algorithm would work step-by-step:
def containsNearbyAlmostDuplicate(numbers, index_difference, value_difference):
if index_difference < 1 or value_difference < 0:
return False
value_map = {}
for index, number in enumerate(numbers):
bucket_number = number // (value_difference + 1)
# Check if bucket exists, implies near duplicate
if bucket_number in value_map:
return True
# Check adjacent buckets for near duplicates
if (bucket_number - 1) in value_map and \
abs(number - value_map[bucket_number - 1]) <= value_difference:
return True
if (bucket_number + 1) in value_map and \
abs(number - value_map[bucket_number + 1]) <= value_difference:
return True
value_map[bucket_number] = number
# Maintain window size based on index_difference
if index >= index_difference:
number_to_remove = numbers[index - index_difference]
bucket_to_remove = number_to_remove // (value_difference + 1)
del value_map[bucket_to_remove]
return False
Case | How to Handle |
---|---|
Empty input array | Return false immediately because no indices exist. |
k is zero | Check if t >= abs(nums[i] - nums[i]), which is equivalent to t >= 0, return true if so, else false if input has more than 1 element. |
t is zero | Check if there are duplicate numbers within the range k, return true if found. |
Large k value exceeding array bounds | The solution should not access array elements out of bounds; k effectively becomes array length - 1. |
Large t value that causes integer overflow in difference calculation | Use long type or appropriate comparison methods to prevent integer overflow. |
Array containing all identical elements | The solution correctly handles identical elements by checking indices within range k. |
Negative numbers in the array | Absolute difference handles negative numbers correctly. |
Extreme boundary values (e.g., Integer.MAX_VALUE, Integer.MIN_VALUE) | Use long type or carefully order operations to avoid overflow when calculating absolute differences. |