#K85562. Longest Consecutive Sequence
Longest Consecutive Sequence
Longest Consecutive Sequence
Given an unsorted array of integers, your task is to find the length of the longest consecutive elements sequence. In other words, you must determine the maximum length of a set of numbers that form a sequence of consecutive integers.
For instance, given the array \([100, 4, 200, 1, 3, 2]\), the longest consecutive sequence is \([1, 2, 3, 4]\) with a length of 4. The expected time complexity is \(\mathcal{O}(n)\), where \(n\) is the number of elements.
inputFormat
The first line contains an integer \(n\) (where \(n \ge 0\)) representing the number of elements in the array. The second line contains \(n\) integers separated by spaces.
outputFormat
Output a single integer representing the length of the longest consecutive sequence.
## sample6
100 4 200 1 3 2
4
</p>