#K53727. Longest Contiguous Subarray with Limited Range

    ID: 29596 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 d. Your task is to determine the length of the longest contiguous subarray such that the difference between the maximum and minimum values in that subarray is at most \(d\).

The first line of input contains an integer T indicating the number of test cases. For each test case, the first line contains two integers n (the length of the array) and d (the allowed maximum difference). The next line contains n space-separated integers representing the array elements. For each test case, output a single integer representing the length of the longest contiguous subarray satisfying the condition.

Note: If no subarray with more than one element meets the condition, the answer is 1 (since every single element is a valid subarray).

inputFormat

The input begins with an integer T denoting the number of test cases. Each test case consists of two lines:

  1. The first line contains two space‐separated integers n and d, where n is the number of elements in the array and d is the allowed maximum difference.
  2. The second line contains n space‐separated integers representing the array.

outputFormat

For each test case, output a single line with one integer representing the length of the longest contiguous subarray in which the difference between the maximum and minimum values does not exceed d.

## sample
3
5 2
1 3 2 2 5
6 1
1 2 2 2 1 1
4 0
1 3 5 7
4

6 1

</p>