#K71942. Longest Consecutive Subsequence
Longest Consecutive Subsequence
Longest Consecutive Subsequence
Given an unsorted array of integers, the task is to find the length of the longest subsequence of consecutive integers. In other words, if the array is denoted as (a_1, a_2, \dots, a_n), we need to determine the maximum integer (L) such that there exists an integer (x) for which all numbers (x, x+1, \dots, x+L-1) appear in the array.
The problem can be formally stated as:
[
\max{L \mid \exists x \in \mathbb{Z},; \forall i \in {0, 1, \ldots, L-1},; x+i \in S}
]
where (S) is the set of integers in the given array.
inputFormat
The first line contains a single integer (n), representing the number of elements in the array. The second line contains (n) space-separated integers.
outputFormat
Output a single integer denoting the length of the longest consecutive subsequence found in the array.## sample
6
100 4 200 1 3 2
4