Skip to main content
Back to problems
#2903
Easy Algorithms

Find indices with index and value difference i

Array Two Pointers
60.3% acceptance
Feb 25, 2026
167
18
You are given a 0-indexed integer array nums having length n, an integer indexDifference, and an integer valueDifference. Your task is to find two indices i and j, both in the range [0, n - 1], that satisfy the following conditions: abs(i - j) >= indexDifference, and abs(nums[i] - nums[j]) >= valueDifference 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. Note: i and j may be equal.

Solution

Rust
Time O(n²)
Space O(n)
LeetCode
solution.rs
impl Solution {
  pub fn find_indices(nums: Vec<i32>, index_difference: i32, value_difference: i32) -> Vec<i32> {
    let n = nums.len();
    for i in 0..n {
      for j in 0..n {
        if (i as i32 - j as i32).abs() >= index_difference
          && (nums[i] - nums[j]).abs() >= value_difference
        {
          return vec![i as i32, j as i32];
        }
      }
    }
    vec![-1, -1]
  }
}