#C11553. Longest Contiguous Subarray with Bounded Difference
Longest Contiguous Subarray with Bounded Difference
Longest Contiguous Subarray with Bounded Difference
Given an array of n integers and an integer k, find the length of the longest contiguous subarray such that the absolute difference between the minimum and maximum elements in the subarray is less than or equal to k. In other words, for any contiguous subarray arr[l...r], the condition
\( \max(arr[l...r]) - \min(arr[l...r]) \leq k \)
must hold.
The input is given from standard input (stdin), where the first line contains two integers n and k. The second line contains n space-separated integers representing the array. The output should be a single integer, printed to standard output (stdout), that represents the maximum length of a contiguous subarray satisfying the condition.
inputFormat
The first line contains two integers n
and k
.
The second line contains n
space-separated integers denoting the elements of the array.
It is guaranteed that \(1 \leq n \leq 10^5\) and \(0 \leq k \leq 10^9\). The array elements are integers.
outputFormat
Output a single integer representing the length of the longest contiguous subarray for which the condition \(\max - \min \leq k\) holds.
## sample5 1
1 3 2 2 1
3