#C12757. Longest Consecutive Sequence

    ID: 42219 Type: Default 1000ms 256MiB

Longest Consecutive Sequence

Longest Consecutive Sequence

Given an unsorted array of integers, your task is to find the length of the longest consecutive elements sequence. The consecutive sequence can be in any order, meaning the numbers do not have to appear sequentially in the array. In other words, if the array contains a set \(S\) of integers, you should return the maximum length of a subsequence where every number \(x\) is followed by \(x+1\) (i.e., the sequence is \(x, x+1, x+2, \ldots\)).

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

Note: If the array is empty, return 0.

inputFormat

The input is given as follows:

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

outputFormat

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

## sample
6
100 4 200 1 3 2
4