Ctrl K

First Far-Apart Jump

Medium
A sensor has logged the readings readings (positions are 0-based). You are given two thresholds, min_gap and min_jump. A pair of positions p ≤ q works if q - p ≥ min_gap and |readings[q] - readings[p]| ≥ min_jump. When min_gap = 0, the two positions may coincide. Find a working pair with the smallest possible q. Choose the partner p for that q among positions 0 to q - min_gap as follows: - if the lowest reading among them works, take it (the earliest position if there is a tie); - otherwise take the highest one (again the earliest among equals). Return [p, q], or [-1, -1] if no pair works. Aim for something faster than O(n²).
Example 1
readings=[4, 9, 2, 6, 10], min_gap=2, min_jump=5 → [2, 4]

For q = 2 and q = 3 no allowed earlier reading differs from the current one by 5 or more. For q = 4 the allowed p are 0 to 2; the lowest reading is 2 (p = 2), and 10 - 2 = 8 ≥ 5.

Example 2
readings=[8, 8, 1], min_gap=1, min_jump=6 → [0, 2]

For q = 2 the lowest of 8, 8 is not below 1, but the highest (8, first at p = 0) is 7 higher.

Example 3
readings=[3, 5], min_gap=0, min_jump=0 → [0, 0]

With both thresholds at zero, the pair [0, 0] already works.

Constraints
  • 1 ≤ len(readings) ≤ 10⁵
  • 0 ≤ readings[i] ≤ 10⁹
  • 0 ≤ min_gap ≤ 10⁵
  • 0 ≤ min_jump ≤ 10⁹
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsTwo Boards Holding Water MediumZero-Sum Triples MediumTriple Sum Nearest the Target MediumFour Numbers to a Target MediumNext Arrangement in Order Medium
loading Python…
Input
readings=[4, 9, 2, 6, 10], min_gap=2, min_jump=5
Expected
[2, 4]

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