#C9948. Longest Increasing Subsequence with Fixed Difference
Longest Increasing Subsequence with Fixed Difference
Longest Increasing 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 ). Note that the subsequence does not need to be contiguous in the original array. This problem requires a dynamic programming solution to efficiently compute the longest subsequence meeting the conditions.
inputFormat
Input is read from standard input (stdin). The first line contains an integer ( T ), denoting the number of test cases. Each test case is given in two lines: the first line contains two integers ( N ) and ( K ) where ( N ) is the number of elements in the sequence and ( K ) is the fixed difference. The second line contains ( N ) space-separated integers representing the elements of the sequence.
outputFormat
For each test case, print a single integer on a new line representing the length of the longest subsequence that is strictly increasing with a consecutive difference of exactly ( K ).## sample
3
5 2
1 3 5 7 9
6 1
10 9 8 7 6 5
4 3
10 13 16 19
5
1
4
</p>