#C13641. Longest Consecutive Sequence

    ID: 43202 Type: Default 1000ms 256MiB

Longest Consecutive Sequence

Longest Consecutive Sequence

Given a list of integers, your task is to determine the length of the longest consecutive subsequence. A consecutive subsequence is a set of numbers where each number differs by exactly 1 from its predecessor. For example, for the array [1, 9, 3, 10, 4, 20, 2], the longest consecutive sequence is 1, 2, 3, 4, so the answer is 4.

You must design an algorithm that works in \(O(n)\) time complexity, which is typically achieved by leveraging a hash set (or similar data structure) to track elements.

inputFormat

The input consists of two lines. The first line contains an integer ( n ) which represents the number of elements in the list. If ( n > 0 ), the second line contains ( n ) space-separated integers.

outputFormat

Output a single integer representing the length of the longest consecutive subsequence found in the list.## sample

0
0