#K54307. Longest Subarray with Limited Difference
Longest Subarray with Limited Difference
Longest Subarray with Limited Difference
You are given an array and an integer K. The task is to find the length of the longest contiguous subarray such that the absolute difference between any two elements in the subarray does not exceed K. Formally, for any subarray B[i...j], it must hold that
\( \max(B[i...j]) - \min(B[i...j]) \leq K \).
This problem challenges you to design an efficient algorithm to process multiple test cases. The expected time complexity is O(M) per test case, where M is the size of the array.
Input: An integer representing the number of test cases, followed by test cases. For each test case, the first line contains two integers M and K, where M is the size of the array and K is the allowed maximum difference. The next line contains M space-separated integers representing the array elements.
Output: For each test case, output a single integer in a new line representing the length of the longest subarray satisfying the condition.
inputFormat
The first line of the input contains a single integer T denoting the number of test cases.
For each test case, the first line contains two integers M and K separated by a space.
The second line contains M space-separated integers representing the array.
outputFormat
For each test case, print a single line containing the length of the longest subarray where the absolute difference between any two elements is at most K.
## sample2
5 3
1 5 3 3 2
4 2
10 12 14 10
4
2
</p>