#K44037. Longest Consecutive Subsequence

    ID: 27442 Type: Default 1000ms 256MiB

Longest Consecutive Subsequence

Longest Consecutive Subsequence

Given an array of integers, the task is to find the length of the longest consecutive subsequence of integers. A consecutive subsequence is defined as a set of integers in which each number is exactly one more than its predecessor. Formally, if \(S\) is a set of unique integers, we are looking for the maximum \(k\) such that there exists an integer \(a\) for which \(\{a, a+1, a+2, \ldots, a+k-1\}\subseteq S\). This problem is commonly encountered in coding competitions and can be solved efficiently using hash sets.

inputFormat

The first line of input contains a single integer \(n\) which represents 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 subsequence.

## sample
10
1 9 3 10 4 20 2 2 2 1
4