#C13938. Longest Consecutive Subsequence

    ID: 43531 Type: Default 1000ms 256MiB

Longest Consecutive Subsequence

Longest Consecutive Subsequence

Given a list of integers, find the length of the longest subsequence such that each adjacent pair of numbers in the subsequence has an absolute difference of one. In other words, if the subsequence is \(a_1, a_2, \dots, a_k\), then for every \(1 \leq i < k\), it must hold that \(|a_{i+1} - a_i| = 1\). Note that the order of the numbers does not matter because you can rearrange the list; however, each number is only counted once.

Input: A list of integers provided via standard input.

Output: The length of the longest consecutive subsequence.

Hint: When finding the consecutive chain, consider using a set to avoid duplicate elements. The desired solution should run in O(n) time.

inputFormat

The input is given via standard input (stdin). The first line contains an integer \(n\) --- the number of elements in the list. The second line contains \(n\) space-separated integers.

outputFormat

Output a single integer to standard output (stdout) representing the length of the longest subsequence where consecutive numbers differ by 1.

## sample
9
1 2 3 4 6 7 8 9 10
5

</p>