#C13672. Longest Subarray with Bounded Difference
Longest Subarray with Bounded Difference
Longest Subarray with Bounded Difference
Given an array of integers and an integer \( k \), find the length of the longest contiguous subarray such that the difference between the maximum and minimum elements in the subarray is at most \( k \). In other words, for any valid subarray \( [a_l, a_{l+1}, \ldots, a_r] \), it must hold that
\( \max(a_l, a_{l+1}, \ldots, a_r) - \min(a_l, a_{l+1}, \ldots, a_r) \le k \).
If the array is empty, the answer is 0.
inputFormat
The first line contains two space-separated integers: \( n \) (the number of elements in the array) and \( k \) (the maximum allowed difference). The second line contains \( n \) space-separated integers representing the elements of the array.
outputFormat
Output a single integer indicating the length of the longest contiguous subarray that satisfies the condition.
## sample6 4
1 3 2 5 8 7
4
</p>