#K10101. Longest Consistent Subsequence
Longest Consistent Subsequence
Longest Consistent Subsequence
You are given an array of N integers and an integer K. Your task is to find the length of the longest subsequence (after sorting the array) where the difference between the maximum and minimum element is at most \(K\). In other words, you need to choose a subset of the array such that if \(m\) and \(M\) are the minimum and maximum values in the subset, then \(M - m \le K\), and the number of elements in the subset is as large as possible.
Note: Although the term "subsequence" might sometimes refer to maintaining the original order, here you are allowed to sort the array first. The standard approach is to sort the array and then use a two-pointer (sliding window) technique to find the longest contiguous segment that satisfies the condition.
Input/Output: The input is read from stdin and the output should be written to stdout.
inputFormat
The first line of input contains two integers N and K, where N is the number of elements in the array and K is the maximum allowed difference between the minimum and maximum elements in the subsequence.
The second line contains N space-separated integers representing the array elements.
Constraints:
- 1 \(\le N \le 10^5\)
- 0 \(\le K \le 10^9\)
- Array elements are integers that can be large.
outputFormat
Output a single integer denoting the length of the longest subsequence whose maximum and minimum differ by at most \(K\).
## sample5 3
1 3 6 10 15
2