Magicsheet logo

Find Indices With Index and Value Difference I

Easy
97.6%
Updated 6/1/2025

Asked by 1 Company

Find Indices With Index and Value Difference I

What is this problem about?

The Find Indices With Index and Value Difference I coding problem asks you to find a pair of indices (i,j)(i, j) in an array nums such that two specific conditions are met:

  1. The distance between the indices is at least indexDifference: ijindexDifference|i - j| \ge indexDifference.
  2. The absolute difference between the values at those indices is at least valueDifference: nums[i]nums[j]valueDifference|nums[i] - nums[j]| \ge valueDifference. If such a pair exists, you return the indices; otherwise, you return [-1, -1].

Why is this asked in interviews?

Paytm and other fintech companies use this "Easy" question to test a candidate's ability to implement simple search logic. It is a fundamental check of loop management and constraint handling. While it can be solved with a brute-force approach, it sets the stage for more complex variations where efficiency becomes critical. It evaluates your attention to detail regarding absolute values and index ranges.

Algorithmic pattern used

This problem follows the Brute Force / Enumeration interview pattern. Since the constraints for "Version I" are usually small, a double-nested loop is sufficient.

  1. Initialize a nested loop where ii goes from 0 to n1n-1 and jj goes from ii to n1n-1.
  2. In each iteration, check if the two conditions (ijindexDiff|i-j| \ge indexDiff and nums[i]nums[j]valueDiff|nums[i]-nums[j]| \ge valueDiff) are satisfied.
  3. Return the first valid pair found.

Example explanation

nums = [5, 1, 4, 1], indexDifference = 2, valueDifference = 4

  1. Check (0,2)(0, 2): 02=22|0-2|=2 \ge 2. Values: 54=1<4|5-4|=1 < 4. (Fail)
  2. Check (0,3)(0, 3): 03=32|0-3|=3 \ge 2. Values: 51=44|5-1|=4 \ge 4. (Success!) Result: [0, 3].

Common mistakes candidates make

  • Wrong Index range: Starting jj from 0 instead of ii, which doubles the number of checks unnecessarily (though technically correct for Easy).
  • Misinterpreting "at least": Using strictly greater than (>>) instead of greater than or equal to (\ge).
  • Complexity error: Forgetting to return [-1, -1] if the loops finish without finding a match.

Interview preparation tip

Always look at the constraints (NN). If NN is small (1000\le 1000), O(N2)O(N^2) is acceptable. If NN is large, you must think about O(N)O(N) or O(NlogN)O(N \log N) optimizations, like tracking the min and max values encountered so far.

Similar Questions