#K37652. Longest Contiguous Subsequence with Bounded Difference

    ID: 26024 Type: Default 1000ms 256MiB

Longest Contiguous Subsequence with Bounded Difference

Longest Contiguous Subsequence with Bounded Difference

You are given a sequence of n integers and a threshold k. Your task is to find the length of the longest contiguous subsequence such that the difference between the maximum and minimum elements of the subsequence does not exceed k.

Formally, for a subarray A[i..j] (with 0 ≤ i ≤ j < n), let

$\max(A[i..j]) - \min(A[i..j]) \le k$.

You need to compute the maximum length of such a subarray.

If the sequence is empty (i.e. n = 0), the answer is 0.

inputFormat

The input is read from stdin and consists of two lines:

  1. The first line contains two integers n and k, where n is the number of elements in the sequence and k is the allowed difference threshold.
  2. The second line contains n space-separated integers representing the sequence. If n is 0, the second line may be omitted.

outputFormat

The output is written to stdout and consists of a single integer that denotes the length of the longest contiguous subsequence with the difference between its maximum and minimum values not exceeding k.

## sample
8 3
1 3 6 4 2 3 5 8
4