Skip to content
Problem 220 · LeetCode 220(opens in a new tab)

Contains Duplicate III

HardArrays

Take a list of whole numbers, nums, plus two limits: indexDiff for distance in position and valueDiff for distance in value. Decide whether two different positions i and j exist that are close in both senses: they are at most indexDiff apart, and the numbers stored there differ by at most valueDiff.

Return true if such a pair exists, and false otherwise.

Example 1

Input:  nums = [8, 1, 14, 6, 4], indexDiff = 2, valueDiff = 3
Output: true
Why:    6 and 4 are 1 position apart and differ by 2
2 more examples and the constraints

Example 2

Input:  nums = [2, 10, 2], indexDiff = 1, valueDiff = 1
Output: false
Why:    the two 2s are 2 positions apart; neighbours differ by 8

Example 3

Input:  nums = [-5, 7, -4], indexDiff = 2, valueDiff = 1
Output: true
Why:    -5 and -4 are 2 positions apart and differ by 1

Constraints

  • 2 ≤ length of nums ≤ 100,000
  • −1,000,000,000 ≤ nums[i] ≤ 1,000,000,000
  • 1 ≤ indexDiff ≤ length of nums
  • 0 ≤ valueDiff ≤ 1,000,000,000

Your turn first

Have a go on paper or in your own editor, then reveal the approaches from the simplest up.

Try it on LeetCode(opens in a new tab)
  1. Check every nearby pairO(n · k)
  2. Sorted windowO(n log k)
  3. BucketsO(n)

The idea in one sentence

The pattern, how to spot it, and the best complexity.

Side by side

ApproachTimeRelative work for 100,000 numbers, indexDiff = 100,000Verdict
Check every nearby pairO(n · k)
Times out on big windows
Sorted windowO(n log k)
Passes; needs an ordered set
BucketsO(n)
Best; three lookups each

n = length of nums, k = indexDiff.

Edge cases to test

  • valueDiff = 0. Only exact repeats count; the bucket width is 1, so each value has its own bucket.
  • Negative numbers. [-1, 1] with valueDiff 1 is false: the buckets must round down, giving −1 and 0.
  • Values at both limits. 1,000,000,000 − (−1,000,000,000) overflows a 32-bit int; compute differences in 64 bits.
  • Equal values just out of reach. [5, 100, 200, 5] with indexDiff 2: the two 5s are 3 apart, so false.

Common mistakes

  • Removing the old number before checking, or after inserting the wrong one, so the window is off by one.
  • Using truncating division for the bucket id, which puts −1 and 1 together.
  • Subtracting two ints in Java or C++ without widening, which overflows near the limits.

How to talk through it

  1. 1Clarify. Are both limits inclusive? Can values be negative or huge? Must i and j differ?
  2. 2Give the brute force. Compare each number with the next indexDiff; O(n · k), too slow for big windows.
  3. 3Keep a sorted window. One ceiling lookup per number tells you if any window value is close: O(n log k).
  4. 4Bucket by value. Width valueDiff + 1 means one number per bucket; check three buckets in O(1).
  5. 5Test out loud. Run a negative pair across zero and two equal values just outside the window.
Was this problem useful?Your vote tells us what to improve next.
Found a bug in our code, or something unclear?