#2905
Find Indices With Index and Value Difference II
expert · 1090 · lc medium +32 · verified · 32.6% accepted · 300 likes · top 10%
Description
A 0-indexed integer array nums of length n, an integer indexDifference, and an integer valueDifference are given.
Find any two indices i and j in [0, n - 1] such that:
- abs(i - j) >= indexDifference
- abs(nums[i] - nums[j]) >= valueDifference
Return [i, j] if such indices exist, or [-1, -1] if not.
Note: i and j may be equal.
Example 1:
Input: nums = [5,1,4,1], indexDifference = 2, valueDifference = 4
Output: [0,3]
Explanation: In this example, i = 0 and j = 3 can be selected.
abs(0 - 3) >= 2 and abs(nums[0] - nums[3]) >= 4.
Hence, a valid answer is [0,3].
[3,0] is also a valid answer.
Example 2:
Input: nums = [2,1], indexDifference = 0, valueDifference = 0
Output: [0,0]
Explanation: In this example, i = 0 and j = 0 can be selected.
abs(0 - 0) >= 0 and abs(nums[0] - nums[0]) >= 0.
Hence, a valid answer is [0,0].
Other valid answers are [0,1], [1,0], and [1,1].
Example 3:
Input: nums = [1,2,3], indexDifference = 2, valueDifference = 4
Output: [-1,-1]
Explanation: In this example, it can be shown that it is impossible to find two indices that satisfy both conditions.
Hence, [-1,-1] is returned.
Code
1
2
3