#C11992. Longest Consecutive Sequence

    ID: 41369 Type: Default 1000ms 256MiB

Longest Consecutive Sequence

Longest Consecutive Sequence

You are given an array of n integers. Your task is to determine the length of the longest sequence of consecutive integers that can be formed by rearranging the elements of the array. Note that the sequence does not need to appear in the array as a contiguous subarray; rather, it is the maximum chain of numbers where each number differs by 1 from its predecessor.

For example, given the array [100, 4, 200, 1, 3, 2, 101], one can form the consecutive sequence [1, 2, 3, 4] which has a length of 4. If the array is empty, the answer is 0.

Formally, if the array is denoted as \( A = [a_1, a_2, \dots, a_n] \), you need to find the largest \( k \) such that there exists a set \( \{x, x+1, \dots, x+k-1\} \) contained in \( A \) (after rearrangement, duplicates are allowed but counted once in forming the sequence).

inputFormat

The input is read from stdin and consists of two lines. The first line contains a single integer n indicating the number of elements in the array. The second line contains n space-separated integers representing the elements of the array. If n is 0, the second line may be empty.

outputFormat

Output a single integer to stdout representing the length of the longest consecutive sequence that can be formed from the given array.

## sample
7
100 4 200 1 3 2 101
4