#C8690. Longest Consecutive Subsequence

    ID: 52700 Type: Default 1000ms 256MiB

Longest Consecutive Subsequence

Longest Consecutive Subsequence

Given an unsorted array of integers, your task is to find and output the length of the longest consecutive elements sequence. A consecutive sequence is defined as a set of numbers where each number is exactly 1 greater than the previous number. For example, in the array [100, 4, 200, 1, 3, 2], the longest consecutive sequence is [1, 2, 3, 4] which has a length of 4.

Note: The consecutive sequence does not need to appear contiguously in the array.

The problem can be mathematically described as finding the maximum length (L) such that there exists an integer (x) for which all numbers (x, x+1, \dots, x+L-1) are present in the array.

inputFormat

The first line contains a single 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 in the array.## sample

6
100 4 200 1 3 2
4