#K55117. Longest Subsequence with Fixed Difference
Longest Subsequence with Fixed Difference
Longest Subsequence with Fixed Difference
You are given an integer n, an integer k, and an array of n integers. Your task is to find the length of the longest contiguous subsequence such that the difference between every two consecutive elements is exactly k.
The problem can be formalized as follows:
Given an array a of length n and an integer k, find the maximum integer L such that there exists an index i where the subarray a[i], a[i+1], ..., a[i+L-1] satisfies
$$a[i+1]-a[i] = k,\quad a[i+2]-a[i+1] = k,\quad \ldots,\quad a[i+L-1]-a[i+L-2] = k. $$If n = 0, then the answer is 0. Note that if no two consecutive elements differ by k, the answer is 1 (since any single element is trivially a valid subsequence).
inputFormat
The first line contains two space-separated integers n and k (n may be zero).
The second line contains n space-separated integers representing the elements of the array. If n = 0, the second line may be omitted.
outputFormat
Output a single integer representing the length of the longest contiguous subsequence where the difference between consecutive elements is exactly k.
## sample7 2
1 3 5 7 9 11 13
7