#K54332. Longest Balanced Subarray

    ID: 29730 Type: Default 1000ms 256MiB

Longest Balanced Subarray

Longest Balanced Subarray

Given an array A of n integers and an integer k, find the length of the longest contiguous subarray such that the balance of the subarray, defined by the difference between its maximum and minimum elements, does not exceed k. In other words, for any subarray A[i...j], you need to satisfy:

\(\max(A[i\ldots j]) - \min(A[i\ldots j]) \leq k\)

This problem can be efficiently solved using a sliding window technique with two deques to maintain the minimum and maximum values in the current window.

inputFormat

The input is read from standard input (stdin) and has the following format:

T
n1 k1
a1 a2 ... an1
n2 k2
b1 b2 ... bn2
... 

Where T (1 ≤ T ≤ 105) is the number of test cases. For each test case, the first line contains two integers: n (the number of elements in the array) and k (the maximum allowed balance). The next line contains n space-separated integers representing the array elements.

outputFormat

For each test case, print a single integer on a new line representing the length of the longest contiguous subarray whose difference between the maximum and minimum element does not exceed k.

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

3

</p>