#C9807. Longest Consecutive Subsequence
Longest Consecutive Subsequence
Longest Consecutive Subsequence
Given a list of integers, find the length of the longest subsequence of consecutive integers. Note that the consecutive elements do not need to appear in order in the input list.
Let \(S\) be the set of integers. A consecutive sequence is defined as a sequence \(a, a+1, a+2, \dots, a+k\) such that every element is in \(S\). Your task is to compute the maximum \(k+1\) among all such sequences.
Example: For the input array [100, 4, 200, 1, 3, 2]
, the longest consecutive sequence is [1, 2, 3, 4]
and its length is 4
.
inputFormat
The first line contains an integer \(n\) denoting the number of elements in the array. The second line contains \(n\) space-separated integers.
outputFormat
Output a single integer representing the length of the longest consecutive sequence.
## sample6
100 4 200 1 3 2
4