#K48392. Longest Subarray with Bounded Difference

    ID: 28410 Type: Default 1000ms 256MiB

Longest Subarray with Bounded Difference

Longest Subarray with Bounded Difference

Given an array of integers, your task is to find the length of the longest contiguous subarray such that the difference between the maximum and minimum elements in the subarray is less than or equal to k. Formally, for a subarray A[i...j] of the array, the condition is:

$$\max\{A[i],\dots,A[j]\} - \min\{A[i],\dots,A[j]\} \le k.$$

You need to design an algorithm with an average time complexity close to O(n). This problem can be efficiently solved using a sliding window technique combined with data structures such as deques to keep track of the minimum and maximum values in the current window.

inputFormat

The input consists of two lines. The first line contains two integers n and k, where n is the number of elements in the array and k is the allowed maximum difference. The second line contains n space-separated integers representing the elements of the array.

outputFormat

Output a single integer, the length of the longest subarray that satisfies the condition.

## sample
10 3
1 2 3 4 6 7 8 9 10 11
4

</p>