Description

Given array nums and integers indexDiff and valueDiff, find if there exist two distinct indices i and j such that |i - j| <= indexDiff and |nums[i] - nums[j]| <= valueDiff.

Examples

Input:nums = [1,2,3,1], indexDiff = 3, valueDiff = 0
Output:true
Explanation:

Same values at indices 0 and 3.

Input:nums = [1,5,9,1,5,9], indexDiff = 2, valueDiff = 3
Output:false
Explanation:

No two elements within 2 indices of each other have a value difference of at most 3. For example, nums[0]=1 and nums[1]=5 differ by 4, which exceeds valueDiff.

Input:nums = [8,2,4,6], indexDiff = 2, valueDiff = 2
Output:true
Explanation:

Values 2 and 4 at indices 1 and 2 have a difference of 2 (which equals valueDiff), and their indices are 1 apart (which is ≤ indexDiff of 2).

Constraints

  • 1 ≤ nums.length ≤ 10⁵

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!