#K41662. Longest Consecutive Subsequence

    ID: 26916 Type: Default 1000ms 256MiB

Longest Consecutive Subsequence

Longest Consecutive Subsequence

Given an array of integers, your task is to find the length of the longest subsequence of consecutive integers. A subsequence of consecutive integers is defined as a sequence \(a_1, a_2, \dots, a_k\) where for every \(i\) (\(1 \le i < k\)), \(a_{i+1} = a_i + 1\). The array may contain duplicate values; however, they are considered only once when forming consecutive sequences.

Input Format: The first line contains an integer \(n\) representing the number of elements in the array. The second line contains \(n\) space-separated integers.

Output Format: Output a single integer representing the length of the longest consecutive subsequence.

Example:
Input:
7
1 9 3 10 4 20 2
Output:
4

inputFormat

The first line of standard input contains a single integer (n) ((0 \leq n \leq 10^5)) representing the number of elements. The second line contains (n) space-separated integers.

outputFormat

Print a single integer denoting the length of the longest subsequence of consecutive integers found in the array.## sample

7
1 9 3 10 4 20 2
4