#K34727. Maximum Length of Constrained Non-decreasing Subsequence

    ID: 25374 Type: Default 1000ms 256MiB

Maximum Length of Constrained Non-decreasing Subsequence

Maximum Length of Constrained Non-decreasing Subsequence

You are given an integer array \(B\) of length \(N\) and an integer \(K\). Your task is to find the length of the longest non-decreasing subsequence of \(B\) such that for any two consecutive elements \(x\) and \(y\) in the subsequence, the difference satisfies \(0 \le y - x \le K\). In other words, if you choose a subsequence \(b_{i_1}, b_{i_2}, \ldots, b_{i_m}\) with \(i_1 < i_2 < \ldots < i_m\), then the condition
\(b_{i_{j+1}} - b_{i_j} \le K\) must hold for all valid \(j\).

Note: A subsequence is obtained by deleting zero or more elements from the array without changing the order of the remaining elements.

Example:
For \(N=6\), \(K=3\) and \(B = [1, 3, 2, 6, 4, 5]\), one valid subsequence is \([1, 3, 4, 5]\) which satisfies the conditions and has length 4.

inputFormat

The input is given via standard input (stdin) and consists of two parts:

  1. The first line contains two space-separated integers \(N\) and \(K\), where \(N\) is the number of elements in the array and \(K\) is the maximum allowed difference.
  2. The next set of values are \(N\) space-separated integers representing the array \(B\).

For example: 6 3 1 3 2 6 4 5

outputFormat

Output a single integer to standard output (stdout) which represents the length of the longest non-decreasing subsequence meeting the constraint.

For the sample input above, the correct output is 4.

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

</p>