#K70742. Longest Contiguous Subsequence with Difference One
Longest Contiguous Subsequence with Difference One
Longest Contiguous Subsequence with Difference One
You are given a sequence of n integers. Your task is to find the length of the longest contiguous subsequence in which the absolute difference between every two consecutive elements is exactly 1.
The sequence is given as input, and you need to compute and output a single integer representing the length of the longest such subsequence.
Note: A contiguous subsequence means that the elements must appear consecutively in the array. The difference between consecutive elements must be strictly \(\pm 1\).
For example, given the sequence 1 2 3 2 3 4
, the entire sequence forms a valid subsequence because \(|1-2| = 1, |2-3| = 1, |3-2| = 1, |2-3| = 1, |3-4| = 1\). Therefore, the answer is 6.
inputFormat
The first line of input contains a single integer \(n\) representing the number of elements in the sequence.
The second line contains \(n\) space-separated integers representing the elements of the sequence.
outputFormat
Output a single integer which is the length of the longest contiguous subsequence in which every two consecutive numbers differ by exactly 1.
## sample6
1 2 3 2 3 4
6