#C1249. Longest Subarray with Limited Absolute Difference

    ID: 41922 Type: Default 1000ms 256MiB

Longest Subarray with Limited Absolute Difference

Longest Subarray with Limited Absolute Difference

You are given an array of integers and an integer \(k\). Your task is to find the length of the longest contiguous subarray such that the absolute difference between the minimum and maximum elements in the subarray is at most \(k\). In other words, for any valid subarray, if \(min\) and \(max\) denote the minimum and maximum element respectively, the subarray is valid if and only if \(max - min \leq k\).

Input Specification: The first line contains two integers \(n\) and \(k\), where \(n\) is the number of elements in the array. The second line contains \(n\) space-separated integers representing the elements of the array. Note that \(n\) can be zero, in which case the array is empty.

Output Specification: Output a single integer representing the length of the longest contiguous subarray found.

For example, given the input:

4 4
8 2 4 7

the longest subarray that satisfies the condition has length 2.

inputFormat

The first line of input 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 array elements. If \(n = 0\), then the second line will be absent.

outputFormat

A single integer representing the length of the longest contiguous subarray for which the difference between the maximum and minimum values is at most \(k\).

## sample
4 4
8 2 4 7
2

</p>