#K80727. Longest Subsequence with Fixed Difference
Longest Subsequence with Fixed Difference
Longest Subsequence with Fixed Difference
You are given an array of integers and a fixed integer k. Your task is to find the length of the longest subsequence such that the difference between any two consecutive elements in the subsequence is exactly k.
More formally, let the subsequence be \(a_{1}, a_{2}, \ldots, a_{m}\). Then it must satisfy:
[ a_{i+1} - a_{i} = k \quad \text{for all } 1 \le i < m, ]
You are required to output the maximum possible value of m over all such subsequences. If the given array is empty, output 0.
Note: The subsequence is not necessarily contiguous in the original array.
inputFormat
The input is read from standard input (stdin) with the following format:
n k a1 a2 a3 ... an
Here, n is the length of the array, k is the fixed difference, and a1, a2, ..., an are the integers in the array. When n = 0, the second line will be empty.
outputFormat
Output to standard output (stdout) a single integer which represents the length of the longest subsequence where consecutive elements have a difference equal to k.
## sample6 2
1 3 5 7 9 11
6