#K58922. Longest Subarray with Limited Range
Longest Subarray with Limited Range
Longest Subarray with Limited Range
You are given an array of n integers and an integer k. Your task is to find the length of the longest contiguous subarray such that the difference between the maximum and minimum elements in this subarray does not exceed k. Formally, for a subarray [al, al+1, \dots, ar], it must hold that:
$$\max(a_l, a_{l+1}, \dots, a_r) - \min(a_l, a_{l+1}, \dots, a_r) \le k $$If no such subarray exists, output 0.
inputFormat
The input is given via stdin as follows:
- The first line contains two integers n and k, where n is the number of elements in the array and k is the maximum allowed difference.
- The second line contains n space-separated integers representing the elements of the array.
outputFormat
Output a single integer to stdout representing the length of the longest contiguous subarray that satisfies the condition.
## sample6 2
1 5 3 2 4 6
3