#K1851. Longest Subsequence with Limited Difference

    ID: 24606 Type: Default 1000ms 256MiB

Longest Subsequence with Limited Difference

Longest Subsequence with Limited Difference

You are given an integer N representing the number of elements, an integer K and an array of N integers. Your objective is to find the length of the longest subsequence (not necessarily contiguous) such that the absolute difference between any two elements in the subsequence is at most K. In mathematical terms, for any two elements a and b in the chosen subsequence, the condition $$|a - b| \le K$$ must hold.

Example: For N = 5, K=3 and the array [1, 5, 3, 4, 2], one valid longest subsequence is [1, 3, 4, 2] because the difference between the smallest (1) and largest (4) elements is 3, which is not greater than K.

Your task is to determine and output the length of such a longest subsequence.

inputFormat

The input is provided via stdin and consists of:

  1. A line containing two integers N and K, where N is the number of array elements and K is the maximum allowed absolute difference.
  2. A line containing N space-separated integers representing the array.

outputFormat

The output should be written to stdout and consist of a single integer: the length of the longest subsequence that satisfies the given condition.

## sample
5 3
1 5 3 4 2
4