#K7781. Longest Consecutive Sequence

    ID: 34947 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 sequence that can be formed from the array's elements. This sequence is defined such that each subsequent integer is exactly one more than its predecessor (i.e., a sequence ({a, a+1, a+2, \ldots})). Note that the elements in the input array may not be sorted, and duplicates should be treated as a single occurrence.

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

inputFormat

The input is provided via standard input (stdin) and consists of two lines. The first line contains an integer (n) indicating 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 elements sequence.## sample

7
100 4 200 1 3 2 20
4