#C7025. Longest Subarray with Bounded Difference

    ID: 50851 Type: Default 1000ms 256MiB

Longest Subarray with Bounded Difference

Longest Subarray with Bounded Difference

Given an array of integers and an integer \(k\), find the length of the longest contiguous subarray such that the difference between the maximum and minimum element in the subarray is less than or equal to \(k\). In other words, for any valid subarray \(S\), the condition \(\max(S) - \min(S) \le k\) must hold.

You are required to process multiple test cases. For each test case, compute the longest such subarray and output its length on a separate line.

inputFormat

The input begins with an integer \(T\) on the first line, indicating the number of test cases. Each test case is described in two lines:

  • The first line contains two integers \(k\) and \(n\), where \(k\) is the maximum allowed difference, and \(n\) is the number of elements in the array.
  • The second line contains \(n\) space-separated integers representing the array.

outputFormat

For each test case, output a single line containing one integer: the length of the longest contiguous subarray that satisfies the condition \(\max(S) - \min(S) \le k\).

## sample
3
5 5
1 3 6 7 8
2 5
1 2 1 2 1
3 7
10 1 12 13 3 6 8
4

5 2

</p>