#K68352. Longest Consecutive Sequence

    ID: 32845 Type: Default 1000ms 256MiB

Longest Consecutive Sequence

Longest Consecutive Sequence

You are given a list of integers. Your task is to find the longest consecutive sequence of integers that can be formed from the given numbers. The consecutive sequence is defined as a sequence of numbers \(a, a+1, a+2, \ldots, a+k\) for some integer \(a\) and non-negative integer \(k\). The sequence does not need to appear contiguously in the input, but the consecutive numbers must exist in the list. If there exist multiple sequences with the same maximum length, output the one whose first element appears earliest in the input order.

Note: The input is provided via standard input (stdin) and the answer should be printed to standard output (stdout).

inputFormat

The first line contains a single integer \(n\) indicating the number of elements in the list. The second line contains \(n\) space-separated integers.

outputFormat

Print the longest consecutive sequence as space‐separated integers on one line. If the list is empty, print an empty line.

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