Ctrl K

Far Apart and Different

Easy
You are given an array values and two numbers min_gap and min_diff. Find indices i ≤ j (0-based; i may equal j) such that: - j - i ≥ min_gap: the indices are far apart; - |values[i] - values[j]| ≥ min_diff: the values differ a lot. Return [i, j]. When several pairs fit, the answer is fixed like this: 1. j must be as small as possible; 2. for that j, first try as i the position of the **smallest** value among values[0..j - min_gap] (the leftmost one if it repeats); 3. if that pair does not fit, take the position of the **largest** value among them (again the leftmost one). If no pair fits, return [-1, -1].
Example 1
values=[6, 2, 9, 1], min_gap=2, min_diff=5 → [0, 3]

For j = 2 only i = 0 is allowed, but |9 - 6| = 3 < 5. For j = 3, i may be 0 or 1; the smallest of values[0..1] is 2 at position 1, and 1 - 2 = -1 < 5; the largest is 6 at position 0, and 6 - 1 = 5 ≥ 5. The answer is [0, 3].

Example 2
values=[4, 7], min_gap=0, min_diff=0 → [0, 0]

With both requirements at zero, j = 0 already works, paired with itself.

Example 3
values=[3, 5, 4], min_gap=1, min_diff=3 → [-1, -1]

No two values differ by more than 2, so no pair fits.

Constraints
  • 1 ≤ len(values) ≤ 100
  • 0 ≤ values[i] ≤ 50
  • 0 ≤ min_gap ≤ 100
  • 0 ≤ min_diff ≤ 50
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsSqueeze Repeats Out of a Sorted Array EasyDrop the Unwanted Value EasyMerge Into the Spare Tail EasyZeros to the Back EasyValues in Both Lists Easy
loading Python…
Input
values=[6, 2, 9, 1], min_gap=2, min_diff=5
Expected
[0, 3]

Run checks the first 3 tests in your browser. Submit checks all 95 tests on the server, hidden ones included.