#C3360. Longest Subarray with Limited Range

    ID: 46779 Type: Default 1000ms 256MiB

Longest Subarray with Limited Range

Longest Subarray with Limited Range

You are given an array of n integers and an integer k. Your task is to determine the length of the longest contiguous subarray such that the difference between the maximum and minimum values in that subarray does not exceed k. In other words, find the maximum integer L for which there exists a subarray a[i...j] satisfying

[ \max(a[i\ldots j]) - \min(a[i\ldots j]) \le k ]

This problem requires an efficient approach to sliding window processing and can be solved using double-ended queues (deques) to maintain the current window's minimum and maximum values.

inputFormat

The input is read from standard input (stdin). It consists of two lines:

  1. The first line contains two integers n and k separated by a space, where n is the number of elements in the array and k is the allowed maximum difference.
  2. The second line contains n integers separated by spaces representing the elements of the array.

outputFormat

Output a single integer to standard output (stdout) – the length of the longest contiguous subarray for which the difference between the maximum and minimum elements is at most k.## sample

7 2
1 3 2 6 4 6 8
3