#K71582. Longest Consecutive Sequence Length

    ID: 33563 Type: Default 1000ms 256MiB

Longest Consecutive Sequence Length

Longest Consecutive Sequence Length

You are given a list of n integers. Your task is to find the length of the longest consecutive subsequence that can be formed from these integers. Note that the consecutive sequence does not have to appear contiguously in the input list. For example, if the input list is 1 9 3 10 4 20 2, the longest consecutive sequence is 1, 2, 3, 4 with a length of 4.

The sequence is defined by the formula \(a, a+1, a+2, \ldots\), where each element must be present in the array. If the list is empty, the answer is \(0\).

Examples:

  • Input: 7 followed by 1 9 3 10 4 20 2 produces output 4.
  • Input: 5 followed by 36 41 56 35 37 produces output 3.

inputFormat

The first line of input contains an integer n representing the number of elements in the list. The second line contains n space-separated integers.

outputFormat

Output a single integer representing the length of the longest consecutive sequence.

## sample
7
1 9 3 10 4 20 2
4

</p>