#K88967. Longest Contiguous Subsequence With Score Difference Limit

    ID: 37426 Type: Default 1000ms 256MiB

Longest Contiguous Subsequence With Score Difference Limit

Longest Contiguous Subsequence With Score Difference Limit

You are given a sequence of integer scores and an integer \(k\). Your task is to determine the length of the longest contiguous subsequence such that the difference between the maximum and minimum score in that subsequence does not exceed \(k\). This problem requires efficient handling of sliding window techniques to compute the desired subsequence length.

Detailed Explanation:

  • Let the subsequence be a contiguous segment from the array.
  • The condition to be satisfied is: \(\max(subsequence) - \min(subsequence) \le k\).
  • You need to output the maximum length of such a subsequence for each test case.

The input will consist of multiple test cases.

inputFormat

The first line contains an integer (T), the number of test cases. For each test case, the first line contains two integers (n) and (k) ((n) is the number of scores, and (k) is the maximum allowed difference). The next line contains (n) space-separated integers representing the scores.

outputFormat

For each test case, output a single line containing the length of the longest contiguous subsequence in which the difference between the maximum and minimum score does not exceed (k).## sample

2
6 3
1 3 5 2 6 8
5 2
4 5 6 3 2
3

3

</p>