#C12071. Longest Consecutive Sequence

    ID: 41458 Type: Default 1000ms 256MiB

Longest Consecutive Sequence

Longest Consecutive Sequence

Given a list of integers, your task is to compute the length of the longest consecutive sequence that can be formed from these integers. The integers in the list may be unsorted and may contain duplicates. If the list is empty, the output should be 0.

The consecutive sequence does not require the numbers to be adjacent in the input; rather, they just need to be consecutive in value. More formally, if the sequence begins with an integer \(a\), then the sequence is \(a, a+1, a+2, \dots, a+k\) for some non-negative integer \(k\), and your task is to find the maximum possible value of \(k+1\) across all sequences.

Input Format: The input is read from standard input (stdin) and consists of two lines. The first line contains a single integer \(n\), representing the number of elements in the list. The second line contains \(n\) space-separated integers.

Output Format: Output the length of the longest consecutive sequence to standard output (stdout).

Be sure to handle edge cases such as an empty list or cases with repeated numbers.

inputFormat

The first line of input consists of an integer \(n\) which indicates the number of elements. The second line consists of \(n\) space-separated integers.

outputFormat

Output a single integer representing the length of the longest consecutive sequence found in the given list.

## sample
6
100 4 200 1 3 2
4

</p>