#K44427. Longest Consecutive Subsequence

    ID: 27529 Type: Default 1000ms 256MiB

Longest Consecutive Subsequence

Longest Consecutive Subsequence

Given a list of integers, your task is to find the length of the longest subsequence of consecutive integers that can be formed from the list. A consecutive subsequence is a sequence of numbers where each number is exactly one more than the previous number.

For example, given the list [100, 4, 200, 1, 3, 2], one of the longest consecutive subsequences is [1, 2, 3, 4], so the answer is 4.

The expected time complexity for an optimal solution is \( \mathcal{O}(n) \), where \( n \) is the number of elements in the list.

inputFormat

The input is given through standard input (stdin) in the following format:

  1. The first line contains an integer \( n \) representing the number of elements in the list.
  2. The second line contains \( n \) space-separated integers.

outputFormat

Output a single integer to standard output (stdout) which is the length of the longest consecutive subsequence.

## sample
6
100 4 200 1 3 2
4

</p>