Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on array fundamentals.
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, andabs(nums[i] - nums[j]) >= valueDifferenceReturn 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.
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.
Constraints:
1 <= n == nums.length <= 1000 <= nums[i] <= 500 <= indexDifference <= 1000 <= valueDifference <= 50Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Two Pointers
[5,1,4,1] 2 4
[2,1] 0 0
[1,2,3] 2 4
minimum-absolute-difference-between-elements-with-constraint)find-indices-with-index-and-value-difference-ii)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2903: Find Indices With Index and Value Difference I
class Solution {
public int[] findIndices(int[] nums, int indexDifference, int valueDifference) {
int mi = 0;
int mx = 0;
for (int i = indexDifference; i < nums.length; ++i) {
int j = i - indexDifference;
if (nums[j] < nums[mi]) {
mi = j;
}
if (nums[j] > nums[mx]) {
mx = j;
}
if (nums[i] - nums[mi] >= valueDifference) {
return new int[] {mi, i};
}
if (nums[mx] - nums[i] >= valueDifference) {
return new int[] {mx, i};
}
}
return new int[] {-1, -1};
}
}
// Accepted solution for LeetCode #2903: Find Indices With Index and Value Difference I
func findIndices(nums []int, indexDifference int, valueDifference int) []int {
mi, mx := 0, 0
for i := indexDifference; i < len(nums); i++ {
j := i - indexDifference
if nums[j] < nums[mi] {
mi = j
}
if nums[j] > nums[mx] {
mx = j
}
if nums[i]-nums[mi] >= valueDifference {
return []int{mi, i}
}
if nums[mx]-nums[i] >= valueDifference {
return []int{mx, i}
}
}
return []int{-1, -1}
}
# Accepted solution for LeetCode #2903: Find Indices With Index and Value Difference I
class Solution:
def findIndices(
self, nums: List[int], indexDifference: int, valueDifference: int
) -> List[int]:
mi = mx = 0
for i in range(indexDifference, len(nums)):
j = i - indexDifference
if nums[j] < nums[mi]:
mi = j
if nums[j] > nums[mx]:
mx = j
if nums[i] - nums[mi] >= valueDifference:
return [mi, i]
if nums[mx] - nums[i] >= valueDifference:
return [mx, i]
return [-1, -1]
// Accepted solution for LeetCode #2903: Find Indices With Index and Value Difference I
impl Solution {
pub fn find_indices(nums: Vec<i32>, index_difference: i32, value_difference: i32) -> Vec<i32> {
let index_difference = index_difference as usize;
let mut mi = 0;
let mut mx = 0;
for i in index_difference..nums.len() {
let j = i - index_difference;
if nums[j] < nums[mi] {
mi = j;
}
if nums[j] > nums[mx] {
mx = j;
}
if nums[i] - nums[mi] >= value_difference {
return vec![mi as i32, i as i32];
}
if nums[mx] - nums[i] >= value_difference {
return vec![mx as i32, i as i32];
}
}
vec![-1, -1]
}
}
// Accepted solution for LeetCode #2903: Find Indices With Index and Value Difference I
function findIndices(nums: number[], indexDifference: number, valueDifference: number): number[] {
let [mi, mx] = [0, 0];
for (let i = indexDifference; i < nums.length; ++i) {
const j = i - indexDifference;
if (nums[j] < nums[mi]) {
mi = j;
}
if (nums[j] > nums[mx]) {
mx = j;
}
if (nums[i] - nums[mi] >= valueDifference) {
return [mi, i];
}
if (nums[mx] - nums[i] >= valueDifference) {
return [mx, i];
}
}
return [-1, -1];
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair of elements. The outer loop picks one element, the inner loop scans the rest. For n elements that is n × (n−1)/2 comparisons = O(n²). No extra memory — just two loop variables.
Each pointer traverses the array at most once. With two pointers moving inward (or both moving right), the total number of steps is bounded by n. Each comparison is O(1), giving O(n) overall. No auxiliary data structures are needed — just two index variables.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Advancing both pointers shrinks the search space too aggressively and skips candidates.
Usually fails on: A valid pair can be skipped when only one side should move.
Fix: Move exactly one pointer per decision branch based on invariant.