#K55217. Longest Contiguous Subarray Within Limit

    ID: 29927 Type: Default 1000ms 256MiB

Longest Contiguous Subarray Within Limit

Longest Contiguous Subarray Within Limit

You are given an array of integers scores and an integer k. Your task is to find the length of the longest contiguous subarray such that the difference between the maximum and minimum elements of the subarray is at most k. Formally, if max and min are the maximum and minimum values in a subarray, then the subarray is valid if

maxmink\max - \min \le k

If no valid subarray exists, return 0. This problem tests your ability to implement an efficient sliding window technique, typically using double-ended queues (deques) to keep track of the minimum and maximum within the current window.

Example:

Input:
7 3
1 3 6 7 9 2 10
Output:
3

inputFormat

The first line contains two integers n and k, where n is the number of scores in the array and k is the limit for the difference between the maximum and minimum. The second line contains n space-separated integers representing the array scores. If n is 0, the second line may be omitted.

outputFormat

Output a single integer — the length of the longest contiguous subarray where the difference between the maximum and minimum scores is at most k.

## sample
7 3
1 3 6 7 9 2 10
3