#C2892. Longest Contiguous Subsequence

    ID: 46258 Type: Default 1000ms 256MiB

Longest Contiguous Subsequence

Longest Contiguous Subsequence

Given a sequence of integers, find the length of the longest contiguous subsequence such that the absolute difference between any two consecutive elements is at most 1. The condition can be formally written in \( \LaTeX \) as:

\( |a_i - a_{i-1}| \leq 1 \) for all valid indices in the subsequence.

The subsequence must be contiguous, meaning the elements appear consecutively in the original sequence.

For example, for the sequence [1, 2, 2, 3, 5, 6, 7], one of the valid contiguous subsequences is [1, 2, 2, 3] with a length of 4.

inputFormat

The first line of input contains a single integer \( n \) representing the number of elements in the sequence. If \( n > 0 \), the second line contains \( n \) space-separated integers representing the array. If \( n = 0 \), there will be no second line.

Input is provided via standard input (stdin).

outputFormat

Output a single integer representing the length of the longest contiguous subsequence where for any two consecutive elements \( a_i \) and \( a_{i-1} \), the condition \( |a_i - a_{i-1}| \leq 1 \) holds.

Output is printed to standard output (stdout).

## sample
1
5
1