Contains Duplicate Iii
To solve this coding challenge, we need to design an algorithm that checks if there exists any pair of indices \( (i, j) \) meeting the given conditions. Let's break this task down step by step to understand the methodology and then provide the pseudocode with extensive comments.
Step-by-Step Explanation
- Understanding the Objective :
-
We need to determine if there are two distinct indices \( i \) and \( j \) in the array
such that:
nums - \( i \neq j \)
- \( |i - j| \leq \text{indexDiff} \)
- \( |\text{nums}[i] - \text{nums}[j]| \leq \text{valueDiff} \)
- Constraints :
- Array length constraints \( 2 \leq \text{nums.length} \leq 10^5 \)
- Value constraints \( -10^9 \leq \text{nums}[i] \leq 10^9 \)
- \( 1 \leq \text{indexDiff} \leq \text{nums.length} \)
- \( 0 \leq \text{valueDiff} \leq 10^9 \)
- Initial Thoughts :
- Using a brute-force approach, comparing every possible pair may not be efficient due to the size constraints.
- Instead, we need a more efficient algorithm using a sliding window and a hash-based approach for quick lookups.
- Sliding Window Approach with Bucketing :
-
We'll use a hash map to keep track of elements within the current sliding window of
.
indexDiff -
Each element
is mapped to a "bucket" based on its value divided by
nums[i]. This helps manage the range efficiently.(valueDiff + 1) - Pseudocode :
- Function Initialization :
-
The function
takes three parameters:
containsNearbyAlmostDuplicate,nums, andindexDiff.valueDiff -
If
is less than or equal to zero, it returns False immediately because the index difference constraint cannot be satisfied.
indexDiff - Hash Map Initialization :
-
We initialize an empty dictionary
to store elements within the current sliding window. This will help in quickly checking if the conditions are met.
seen_buckets - Iterate Through the Array :
-
Start a loop from 0 to the length of
minus one.
nums -
For each index
, check if the index exceeds the allowed index difference (
i). If it does, remove the element outside the current sliding window fromindexDiff.seen_buckets - Determine Buckets :
-
Compute the current bucket for
by dividing it by
nums[i].(valueDiff + 1) - Check Neighboring Buckets :
-
For each bucket that includes the current bucket, and its immediate neighbors (previous and next bucket), check if there exists a match in
.
seen_buckets - If a matching element is found that satisfies the value difference condition, return True.
- Update the Hash Map :
-
If no matching element is found, update the
with the current element's value.
seen_buckets - Return Result :
- If no pair is found after processing the entire array, return False.
function containsNearbyAlmostDuplicate(nums, indexDiff, valueDiff):
if indexDiff <= 0:
return False
# Dictionary to maintain the sliding window
seen_buckets = {}
# Iterate through each number in the array
for i from 0 to length(nums) - 1:
# Clean the bucket window when it exceeds the indexDiff
if i > indexDiff:
old_bucket = nums[i - indexDiff - 1] // (valueDiff + 1)
delete seen_buckets[old_bucket]
# Find the current bucket of the element
current_bucket = nums[i] // (valueDiff + 1)
# Check the current bucket and its neighboring buckets for possible matches
for bucket in [current_bucket, current_bucket - 1, current_bucket + 1]:
if bucket in seen_buckets and abs(nums[i] - seen_buckets[bucket]) <= valueDiff:
return True
# Insert the current element's value in the bucket map
seen_buckets[current_bucket] = nums[i]
return False