#C14531. Longest Consecutive Sequence

    ID: 44191 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 sequence of consecutive integers that can be formed from the array. Note that the consecutive sequence need not be contiguous in the original array.

The formula for a consecutive sequence is defined as a set \(\{a, a+1, a+2, \dots, a+k\}\) where each element is present in the array. The answer is the maximum \(k+1\) for all possible sequences.

If the array is empty, output 0.

Example:

Input:
6
100 4 200 1 3 2

Output: 4

</p>

inputFormat

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

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

outputFormat

Output a single integer to standard output (stdout), representing the length of the longest consecutive sequence in the array.

## sample
6
100 4 200 1 3 2
4