#C14386. Longest Consecutive Sequence
Longest Consecutive Sequence
Longest Consecutive Sequence
You are given a list of integers, and your task is to determine the length of the longest consecutive sequence present in the list. A consecutive sequence is a set of integers where each number in the sequence increases by one compared to the previous number. For example, in the list [100, 4, 200, 1, 3, 2], the longest consecutive sequence is [1, 2, 3, 4], and its length is 4.
Note: The list might contain duplicate values, and the input can be large. An efficient solution is required with a time complexity close to O(n).
You are encouraged to use appropriate data structures such as hash sets for optimal performance.
Mathematically, if we denote the list as \( A = [a_1, a_2, ..., a_n] \), you are required to find the maximum \( L \) such that there exists a sequence \( x, x+1, ..., x+L-1 \) with each element belonging to \( A \).
inputFormat
The first line of input contains a single integer \( n \) representing the number of elements in the list. The second line contains \( n \) space-separated integers.
If \( n = 0 \), it means the list is empty.
outputFormat
Output a single integer, the length of the longest consecutive sequence found in the list.
## sample6
100 4 200 1 3 2
4