#C12442. Longest Consecutive Sequence

    ID: 41870 Type: Default 1000ms 256MiB

Longest Consecutive Sequence

Longest Consecutive Sequence

Given an array of integers, your task is to find the longest sequence of consecutive integers present in the array. The sequence should be strictly increasing by 1 from one element to the next, and duplicates should be treated as a single occurrence. If multiple sequences of the same maximum length exist, return the one that appears first (based on the smallest starting number found in the array).

For example, given the array [3, 9, 1, 10, 4, 20, 2], the longest consecutive sequence is [1, 2, 3, 4] because these numbers form a continuous interval. Mathematically, a consecutive sequence starting from \(a\) is defined as \(a, a+1, a+2, \ldots, a+k\) for some non-negative integer \(k\).

inputFormat

The first line of input contains a single integer \(n\) (\(n \ge 0\)), representing the number of integers in the array. The second line contains \(n\) space-separated integers.

outputFormat

Output the longest consecutive sequence in ascending order, with each number separated by a single space. If the array is empty, output nothing.

## sample
7
3 9 1 10 4 20 2
1 2 3 4