#K74902. Longest Contiguous Subarray Within \(K\)

    ID: 34300 Type: Default 1000ms 256MiB

Longest Contiguous Subarray Within \(K\)

Longest Contiguous Subarray Within (K)

Given a sequence of plant heights, your task is to identify the length of the longest contiguous subarray in which the difference between the maximum and minimum heights does not exceed a given integer \(K\). In other words, for any subarray \(H[l, r]\), the condition \( \max(H[l,r]) - \min(H[l,r]) \le K\) must hold.

The input contains several datasets. Each dataset starts with two integers \(N\) and \(K\), where \(N\) denotes the number of plants and \(K\) is the maximum allowed difference in heights. The following line contains \(N\) integers representing the heights of the plants. The input terminates with a line containing 0 0.

inputFormat

The input consists of multiple datasets. Each dataset is provided in two lines. The first line contains two integers (N) and (K). The second line contains (N) space-separated integers representing the heights of the plants. The input ends with a line where both (N) and (K) are zero ("0 0").

outputFormat

For each dataset, output a single line containing an integer that represents the length of the longest contiguous subarray where the difference between the maximum and minimum heights is at most (K).## sample

5 2
1 3 2 2 4
0 0
4

</p>