A single straight row in a vineyard holds n soil-moisture sensors, planted at positions 0 through n-1, each reporting an integer moisture reading. The irrigation controller wants to flag two sensors that are far enough apart along the row and whose readings disagree by enough to signal a real difference in soil condition. Given the readings and two thresholds minGap and minDiff, find two distinct sensor positions i < j such that j - i >= minGap and |reading[i] - reading[j]| >= minDiff. Multiple valid pairs may exist; among all of them, report the one with the smallest i, and, among pairs that share that smallest i, the one with the smallest j. If no valid pair exists, report that instead.
n, minGap, and minDiff.n integers, the moisture readings reading[0] through reading[n-1].Print two integers i j (the required pair, with i < j), separated by a single space. If no valid pair exists, print -1 -1.
Example 1
Input
6 3 20 10 12 50 8 40 5
Expected
0 4
Explanation
The smallest possible i is 0 (reading 10). The smallest j with j-0>=3 is j=3 (reading 8), but |10-8|=2 is less than minDiff=20, so that pair fails. The next candidate j=4 (reading 40) gives |10-40|=30>=20, satisfying both conditions, so (0,4) is reported.
Example 2
Input
4 3 5 10 11 9 12
Expected
-1 -1
Explanation
With minGap=3, the only index pair whose positions differ by at least 3 is (0,3): |10-12|=2, which is less than minDiff=5. No other pair even satisfies the gap requirement, so no valid pair exists and the answer is -1 -1.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →