#C6542. Longest Consecutive Subsequence

    ID: 50314 Type: Default 1000ms 256MiB

Longest Consecutive Subsequence

Longest Consecutive Subsequence

Given an array of integers, find the length of the longest consecutive subsequence. A consecutive subsequence is a set of numbers that appear in increasing order such that every two adjacent numbers have a difference of 1 (i.e. \(a_{i+1} - a_i = 1\)). For example, given the array [100, 4, 200, 1, 3, 2, 101], the longest consecutive subsequence is [1, 2, 3, 4] which has a length of 4.

The input will be provided via standard input. The first line contains an integer \(n\) representing the number of elements. The following line contains \(n\) space-separated integers. Your task is to output a single integer representing the length of the longest consecutive subsequence.

If the array is empty (i.e. \(n=0\)), the output should be 0.

inputFormat

The input consists of two lines:

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

outputFormat

Output a single integer denoting the length of the longest consecutive subsequence. The result should be printed on a new line.

## sample
7
100 4 200 1 3 2 101
4