Contains Duplicate III

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

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

Example 1
Inputnums = [1,2,3,1], indexDiff = 3, valueDiff = 0
Outputtrue
We can choose (i, j) = (0, 3) because the indices are different, abs(0 - 3) <= 3, and abs(1 - 1) <= 0.
Example 2
Inputnums = [1,5,9,1,5,9], indexDiff = 2, valueDiff = 3
Outputfalse
After trying all possible pairs (i, j), none satisfies all three conditions.

Constraints

  • 2 <= nums.length <= 10^5
  • -10^9 <= nums[i] <= 10^9
  • 1 <= indexDiff <= nums.length
  • 0 <= valueDiff <= 10^9

Asked at 7 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate