#K49027. Longest Fixed-Difference Subsequence
Longest Fixed-Difference Subsequence
Longest Fixed-Difference Subsequence
You are given a sequence of n integers and an integer d. Your task is to find the length of the longest subsequence such that the absolute difference between every pair of consecutive elements is exactly d
. Specifically, for every consecutive pair of elements in the subsequence, the difference (either positive or negative) should equal d
.
The subsequence does not have to be contiguous, but the order of elements in the sequence must be preserved. Formally, if the subsequence is a1, a2, ..., ak, then for all 1 ≤ i < k, either $$a_{i+1} - a_i = d$$ or $$a_{i+1} - a_i = -d.$$
Your goal is to compute the maximum possible length of such a subsequence.
inputFormat
The first line contains two integers n
and d
, where n
is the number of elements in the sequence and d
is the required difference between consecutive elements of the subsequence.
The second line contains n
integers representing the sequence, separated by spaces.
Example:
6 2 1 3 2 5 7 8
outputFormat
Output a single integer representing the length of the longest subsequence satisfying the conditions.
Example:
4## sample
6 2
1 3 2 5 7 8
4