#C4876. Longest Consecutive Sequence

    ID: 48462 Type: Default 1000ms 256MiB

Longest Consecutive Sequence

Longest Consecutive Sequence

You are given an unsorted array of integers. Your task is to find the length of the longest consecutive elements sequence. The sequence must be consecutive in value (i.e., for a number x in the sequence, x+1 should also be in the sequence), even if the numbers are not adjacent in the original array.

Note: The solution must work with an input read from standard input (stdin) and output the answer to standard output (stdout). Duplicate numbers may appear in the array, but they should be counted only once in the sequence.

The answer should be computed in an efficient manner, ideally in O(n) time complexity.

In mathematical terms, if S is the set of given integers, find the maximum length L such that there exists an integer a with:

\(a, a+1, a+2, \dots, a+L-1 \subseteq S\).

inputFormat

The input is read from standard input (stdin) in the following format:

N
num1 num2 num3 ... numN

Where N is a non-negative integer representing the number of elements in the array, followed by N space-separated integers.

outputFormat

The output should be a single integer, the length of the longest consecutive sequence, printed to standard output (stdout).

## sample
6
100 4 200 1 3 2
4

</p>