Contains Duplicate III - Problem

You're given an integer array nums and two integers indexDiff and valueDiff. Your task is to find if there exists a pair of indices (i, j) that satisfy all of the following conditions:

  • i != j (different indices)
  • abs(i - j) <= indexDiff (indices are close enough)
  • abs(nums[i] - nums[j]) <= valueDiff (values are similar enough)

Return true if such a pair exists, false otherwise.

Example: If nums = [1,2,3,1], indexDiff = 3, and valueDiff = 0, we need to find two identical values within 3 positions of each other. The answer is true because nums[0] = nums[3] = 1 and abs(0-3) = 3 <= 3.

Input & Output

example_1.py โ€” Basic Match
$ Input: nums = [1,2,3,1], indexDiff = 3, valueDiff = 0
โ€บ Output: true
๐Ÿ’ก Note: We find indices i=0 and j=3 where nums[0]=1 and nums[3]=1. The constraints are satisfied: |0-3|=3 โ‰ค 3 and |1-1|=0 โ‰ค 0.
example_2.py โ€” Value Difference
$ Input: nums = [1,5,9,1,5,9], indexDiff = 2, valueDiff = 3
โ€บ Output: false
๐Ÿ’ก Note: No two elements within indexDiff=2 positions have a value difference โ‰ค 3. For example, indices 0,1: |1-5|=4 > 3.
example_3.py โ€” Edge Case
$ Input: nums = [1,3,1], indexDiff = 1, valueDiff = 1
โ€บ Output: false
๐Ÿ’ก Note: Adjacent pairs: (1,3) at indices 0,1 has |1-3|=2 > 1, and (3,1) at indices 1,2 has |3-1|=2 > 1. No valid pair exists.

Visualization

Tap to expand
๐Ÿ™๏ธ Smart City Security NetworkCity Street๐Ÿ“นCam ANoise: 45dB๐Ÿ“นCam BNoise: 47dB๐Ÿ“นCam CNoise: 52dB๐Ÿ“นCam DNoise: 46dBโœ“ MATCH DETECTEDDistance: 200m โ‰ค 250m โœ“Noise Diff: |47-46| = 1dB โ‰ค 3dB โœ“๐ŸŽฏ Problem Parameters:โ€ข Max Distance (indexDiff): 250mโ€ข Max Noise Diff (valueDiff): 3dB๐Ÿ”ง Bucket Strategy:โ€ข Bucket Size: 3dB + 1 = 4dBโ€ข Bucket 11: [44-47dB] โ† Cams A,B,Dโ€ข Bucket 13: [52-55dB] โ† Cam Cโšก O(n) Solution: Same bucket = instant match!๐Ÿ“Š Algorithm Comparison:๐Ÿ”จ Brute Force: O(nยฒ) - Check all pairs๐Ÿ” Binary Search: O(n log k) - Sorted windowโšก Bucket Sort: O(n) - Smart groupingFor n=20,000 cameras:โ€ข Brute: ~400M comparisonsโ€ข Binary: ~60K comparisonsโ€ข Bucket: ~20K comparisons
Understanding the Visualization
1
Deploy Cameras
Place cameras at different positions, each recording noise levels
2
Set Parameters
Define maximum distance between cameras and acceptable noise difference
3
Monitor Window
Use sliding window to only check cameras within distance limit
4
Bucket Classification
Group similar noise levels into buckets for efficient comparison
5
Detection
Alert when two cameras meet both distance and noise similarity criteria
Key Takeaway
๐ŸŽฏ Key Insight: By treating the problem as a smart city monitoring system, we see how bucket sort creates 'zones' of similar values, allowing instant detection of nearby cameras with similar readings, achieving optimal O(n) performance.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n)

We process each element exactly once, and hash map operations (insert, delete, lookup) are O(1) on average

n
2n
โœ“ Linear Growth
Space Complexity
O(min(n, indexDiff))

The hash map stores at most indexDiff+1 elements at any time, which is bounded by min(n, indexDiff)

n
2n
โšก Linearithmic Space

Constraints

  • 2 โ‰ค nums.length โ‰ค 2 ร— 104
  • -231 โ‰ค nums[i] โ‰ค 231 - 1
  • 0 โ‰ค indexDiff โ‰ค nums.length
  • 0 โ‰ค valueDiff โ‰ค 231 - 1
Asked in
Google 45 Amazon 38 Meta 32 Microsoft 28
52.4K Views
Medium-High Frequency
~25 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen