#C13880. Longest Consecutive Sequence

    ID: 43467 Type: Default 1000ms 256MiB

Longest Consecutive Sequence

Longest Consecutive Sequence

Given an unsorted array of integers, find the length of the longest consecutive elements sequence.

The sequence is defined as a set of numbers that can be arranged in order such that each consecutive pair differs by exactly \(1\). Note that the input array may contain duplicates. A solution with a time complexity of \(O(n)\) is expected.

For example, given the array [100, 4, 200, 1, 3, 2], the longest consecutive sequence is [1, 2, 3, 4] and its length is 4.

inputFormat

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

If \(n = 0\), the second line will be empty.

outputFormat

Output a single integer which is the length of the longest consecutive sequence in the array.

## sample
6
100 4 200 1 3 2
4

</p>