#C13283. Longest Consecutive Sequence
Longest Consecutive Sequence
Longest Consecutive Sequence
You are given an unsorted array of n integers. Your task is to find the length of the longest consecutive elements sequence.
Consecutive elements are numbers that appear in a sequence where each number is exactly 1 greater than the previous number. For example, in the array [100, 4, 200, 1, 3, 2], the longest consecutive sequence is [1, 2, 3, 4] and its length is 4.
Your solution should ideally have a time complexity of \(O(n)\) on average.
inputFormat
The input is given via standard input (stdin) with the following format:
- 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 representing the length of the longest consecutive elements sequence.
## sample6
100 4 200 1 3 2
4
</p>