#C13731. Longest Contiguous Subarray with Limited Range

    ID: 43302 Type: Default 1000ms 256MiB

Longest Contiguous Subarray with Limited Range

Longest Contiguous Subarray with Limited Range

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 difference between the maximum and minimum values in this subarray is less than or equal to (k).

In other words, if the subarray is denoted by (A[i \dots j]), you need to ensure that: [ \max(A[i \dots j]) - \min(A[i \dots j]) \leq k ] and maximize (j - i + 1).

If the array is empty, the answer is 0.

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 range. If (n > 0), the second line contains (n) space-separated integers representing the array. If (n = 0), there will be no second line.

outputFormat

Output a single integer representing the length of the longest contiguous subarray that satisfies the condition.## sample

8 4
1 3 6 7 9 4 10 5
3

</p>