#C13814. Longest Consecutive Subsequence
Longest Consecutive Subsequence
Longest Consecutive Subsequence
You are given an array of integers. Your task is to determine the length of the longest subsequence of consecutive integers present in the array. A subsequence of consecutive integers means that if an integer x is in the subsequence, then x+1 should also appear, and so on. Note that the consecutive numbers do not need to appear in order in the original array.
For example, given the input array [100, 4, 200, 1, 3, 2]
, the longest consecutive subsequence is [1, 2, 3, 4]
with a length of 4.
Note: In this problem, the input is provided via stdin and the output is expected on stdout.
The mathematical description of the consecutive property can be stated as follows: For a set \( S \) of integers, for any \( x \in S \), if \( x - 1 \notin S \), then starting from \( x \) the length of the consecutive sequence is given by finding the maximum \( k \) such that \( \{ x, x+1, \ldots, x+k-1 \} \subseteq S \). The answer is the maximum \( k \) over all such starting points.
inputFormat
The first line of the input contains an integer \( n \) indicating the number of elements in the array. The second line contains \( n \) space-separated integers representing the elements of the array.
Example:
6 100 4 200 1 3 2
outputFormat
Output a single integer, which is the length of the longest consecutive subsequence in the array.
Example:
4## sample
6
100 4 200 1 3 2
4