Pilots
Time limit3sMemory limit128 MB
Find the length of the longest contiguous fragment whose maximum minus minimum is at most t.
- Level
Medium4 of 10
- Topics
- Sliding window, Two pointers, Queue
- Solved
- No attempts yet
Problem
At the Byteotian Training Centre, pilots train for missions that demand extraordinary precision and control. One measure of a pilot's skill is how long they can fly a desired route without deviating too much, that is, how steadily they can hold their course.
The task is hard because the simulator is extremely sensitive: it records even the slightest movement of the yoke (the control device used to steer an aircraft). At every moment the simulator stores a single integer describing the yoke's position.
Before each session a tolerance level is fixed. The pilot must then fly for as long as possible so that every recorded yoke position stays within of every other. Formally, the flight fragment from moment to moment is within tolerance when the measurements satisfy for every pair of elements in it.
Given the tolerance level and the sequence of yoke position measurements, determine the length of the longest flight fragment that stays within the tolerance level .
Input
The first line contains two integers and (, ), separated by a single space: the tolerance level and the number of recorded measurements.
The second line contains those measurements separated by single spaces. Each measurement is an integer between and .
Output
Print a single integer: the maximum length of a flight fragment that stays within the given tolerance level .