#K58142. Longest Contiguous Subarray with Limited Range Difference

    ID: 30577 Type: Default 1000ms 256MiB

Longest Contiguous Subarray with Limited Range Difference

Longest Contiguous Subarray with Limited Range Difference

You are given an integer T representing the number of test cases. For each test case, you are given two integers N and D and an array ratings of N integers. Your task is to find the length of the longest contiguous subarray such that the difference between the maximum and minimum values in that subarray is at most D.

Formally, for a given array A of length N and a non-negative integer D, you need to determine the maximum length L such that there exists an index pair (i, j) with 0 ≤ i ≤ j < N where j - i + 1 = L and

\( \max\{ A[i], A[i+1], \dots, A[j] \} - \min\{ A[i], A[i+1], \dots, A[j] \} \le D \).

The input is read from standard input and the output is printed to standard output. Ensure that your solution is efficient and can handle large inputs.

inputFormat

The input begins with a single integer T, the number of test cases. Each test case is given in the following format:

  • The first line contains two integers N and D, where N is the number of elements in the array and D is the allowed maximum difference.
  • The second line contains N space-separated integers representing the array ratings.

All input is taken from standard input.

outputFormat

For each test case, output a single integer on a new line representing the length of the longest contiguous subarray that satisfies the condition. The output should be printed to standard output.

## sample
3
6 3
1 3 6 7 9 4
5 0
7 7 7 7 7
4 10
1 3 6 10
3

5 4

</p>