A machine logs a daily calibration reading for n days. Define the drift after day i as (maximum reading among days 1..i) - (minimum reading among days 1..i). Given a tolerance value L, find the smallest day index i (1-indexed) at which the drift strictly exceeds L (drift > L). If the drift never exceeds L, even after all n days, print -1.
Line 1: two integers n L.
Line 2: n space-separated integers, the calibration reading for day 1, day 2, ..., day n.
A single integer: the smallest day index whose drift exceeds L, or -1.
Example 1
Input
5 4 10 12 9 15 8
Expected
4
Explanation
Running (max, min, drift): day1 (10,10,0), day2 (12,10,2), day3 (12,9,3), day4 (15,9,6). Drift 6 exceeds 4 on day 4.
Example 2
Input
3 100 1 2 3
Expected
-1
Explanation
The drift only ever reaches 3-1=2, which never exceeds 100, so the answer is -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 →