2905. Find Indices With Index and Value Difference II Medium

@problem@discussion
#Array#Two Pointers



1/**
2 * [2905] Find Indices With Index and Value Difference II
3 *
4 * You are given a 0-indexed integer array nums having length n, an integer indexDifference, and an integer valueDifference.
5 * Your task is to find two indices i and j, both in the range [0, n - 1], that satisfy the following conditions:
6 * 
7 * 	abs(i - j) >= indexDifference, and
8 * 	abs(nums[i] - nums[j]) >= valueDifference
9 * 
10 * Return an integer array answer, where answer = [i, j] if there are two such indices, and answer = [-1, -1] otherwise. If there are multiple choices for the two indices, return any of them.
11 * Note: i and j may be equal.
12 *  
13 * <strong class="example">Example 1:
14 * 
15 * Input: nums = [5,1,4,1], indexDifference = 2, valueDifference = 4
16 * Output: [0,3]
17 * Explanation: In this example, i = 0 and j = 3 can be selected.
18 * abs(0 - 3) >= 2 and abs(nums[0] - nums[3]) >= 4.
19 * Hence, a valid answer is [0,3].
20 * [3,0] is also a valid answer.
21 * 
22 * <strong class="example">Example 2:
23 * 
24 * Input: nums = [2,1], indexDifference = 0, valueDifference = 0
25 * Output: [0,0]
26 * Explanation: In this example, i = 0 and j = 0 can be selected.
27 * abs(0 - 0) >= 0 and abs(nums[0] - nums[0]) >= 0.
28 * Hence, a valid answer is [0,0].
29 * Other valid answers are [0,1], [1,0], and [1,1].
30 * 
31 * <strong class="example">Example 3:
32 * 
33 * Input: nums = [1,2,3], indexDifference = 2, valueDifference = 4
34 * Output: [-1,-1]
35 * Explanation: In this example, it can be shown that it is impossible to find two indices that satisfy both conditions.
36 * Hence, [-1,-1] is returned.
37 *  
38 * Constraints:
39 * 
40 * 	1 <= n == nums.length <= 10^5
41 * 	0 <= nums[i] <= 10^9
42 * 	0 <= indexDifference <= 10^5
43 * 	0 <= valueDifference <= 10^9
44 * 
45 */
46pub struct Solution {}
47
48// problem: https://leetcode.com/problems/find-indices-with-index-and-value-difference-ii/
49// discuss: https://leetcode.com/problems/find-indices-with-index-and-value-difference-ii/discuss/?currentPage=1&orderBy=most_votes&query=
50
51// submission codes start here
52
53impl Solution {
54    pub fn find_indices(nums: Vec<i32>, index_difference: i32, value_difference: i32) -> Vec<i32> {
55        vec![]
56    }
57}
58
59// submission codes end
60
61#[cfg(test)]
62mod tests {
63    use super::*;
64
65    #[test]
66    fn test_2905() {
67    }
68}
69


Back
© 2025 bowen.ge All Rights Reserved.