#C5311. Longest Valid Subsequence
Longest Valid Subsequence
Longest Valid Subsequence
You are given an array of n integers and an integer k. Your task is to find the length of the longest subsequence such that the subsequence is sorted in increasing order and the difference between any two consecutive elements is at least k.
The condition for any two consecutive elements a and b in the subsequence is given by the inequality:
\(b - a \geq k\)
The subsequence does not have to be contiguous in the original array. However, to facilitate the process, you are allowed to sort the array as part of your solution.
Note: When k is 0, every increasing sequence is valid, and the answer becomes n (the size of the array) because every element can be chosen.
inputFormat
The first line of input contains two integers n and k separated by a space, where n is the number of elements in the array and k is the minimum required difference.
The second line contains n space-separated integers representing the array elements.
outputFormat
Output a single integer representing the length of the longest valid subsequence.
## sample5 3
1 5 3 9 7
3
</p>