#K80852. Longest Consecutive Sequence
Longest Consecutive Sequence
Longest Consecutive Sequence
You are given a list of integers. Your task is to find the longest sequence of consecutive integers present in the list and output its length along with the sequence itself.
If there exist multiple sequences with the same maximum length, you should choose the one with the smallest starting number.
For example, given the list [4, 2, 1, 6, 5], the longest consecutive sequence is [4, 5, 6] with a length of 3.
The intended time complexity of an optimal solution is \(O(n)\).
inputFormat
The input is given via standard input (stdin) in the following format:
- The first line contains a single integer \(n\) representing the number of elements in the list.
- The second line contains \(n\) space-separated integers. If \(n = 0\), the second line will be empty.
outputFormat
Output to standard output (stdout) in two lines:
- The first line contains a single integer representing the length of the longest consecutive sequence.
- The second line contains the elements of the longest consecutive sequence separated by a single space. If there is no consecutive sequence, output an empty second line.
0
0
</p>