#C2741. Longest Consecutive Sequence

    ID: 46091 Type: Default 1000ms 256MiB

Longest Consecutive Sequence

Longest Consecutive Sequence

Given an unsorted array of integers, your task is to determine the length of the longest sequence of consecutive numbers. A consecutive sequence is defined as a sequence in which the difference between two adjacent numbers is exactly 1. Note that the array may contain duplicate values, but they should be considered only once when forming the sequence.

The problem can be mathematically summarized using the following concept. Let \(S\) be a set derived from the array, and a consecutive sequence starting at \(x\) is defined as \(\{x, x+1, x+2, \ldots, x+k\}\) such that \(x-1 \notin S\) and \(x+i \in S\) for all \(0 \leq i \leq k\). Your goal is to find the maximum \(k+1\) over all possible sequences.

For example, in the array [100, 4, 200, 1, 3, 2], the longest consecutive sequence is [1, 2, 3, 4] with a length of 4.

inputFormat

Input is read from stdin. The first line contains an integer \(n\) (the number of elements in the array). The second line contains \(n\) space-separated integers representing the elements of the array.

outputFormat

Output the length of the longest consecutive sequence found in the array to stdout.

## sample
6
100 4 200 1 3 2
4

</p>