#K8961. Longest Consecutive Sequence
Longest Consecutive Sequence
Longest Consecutive Sequence
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. The consecutive sequence must be comprised of integers that can be arranged in an increasing order with a difference of 1 between adjacent numbers. Note that the elements of the sequence do not have to appear consecutively in the given array.
For example, in the array [100, 4, 200, 1, 3, 2]
, the longest consecutive sequence is [1, 2, 3, 4]
, so the answer is 4
.
The solution should handle cases with negative numbers, duplicates, and empty arrays.
inputFormat
The first line contains a single integer n (0 ≤ n ≤ 105), which represents the number of elements in the array.
If n is greater than zero, the second line contains n space-separated integers representing the elements of the array.
outputFormat
Output a single integer which is the length of the longest consecutive sequence in the given array.
## sample6
100 4 200 1 3 2
4
</p>