#K7326. Longest Consecutive Sequence

    ID: 33936 Type: Default 1000ms 256MiB

Longest Consecutive Sequence

Longest Consecutive Sequence

Given a list of integers, your task is to find the length of the longest consecutive elements sequence.

A consecutive sequence is defined as a subset of numbers \(\{a, a+1, a+2, \ldots, a+k\}\) where each adjacent pair differs by 1. Formally, if \(S\) is the set of integers, you need to compute

\(\max_{s \subseteq S} \{\text{length}(s)\}\)

For example, given the set \(\{100, 4, 200, 1, 3, 2\}\), the longest consecutive sequence is \(\{1, 2, 3, 4\}\) and its length is 4.

inputFormat

The input begins with an integer \(n\) on the first line representing the number of elements in the array. The second line contains \(n\) space-separated integers.

outputFormat

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

## sample
6
100 4 200 1 3 2
4