#K75167. Longest Consecutive Sequence

    ID: 34360 Type: Default 1000ms 256MiB

Longest Consecutive Sequence

Longest Consecutive Sequence

Given an unsorted array of integers, determine the length of the longest consecutive elements sequence. Two numbers are consecutive if their difference is 1.

For example, in the array [100, 4, 200, 1, 3, 2], the longest consecutive sequence is [1, 2, 3, 4], so the answer is 4.

The expected time complexity is O(n). The solution should read input from standard input and output the result to standard output.

inputFormat

The first line contains an integer n, 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