#K91037. Longest Consecutive Subsequence

    ID: 37886 Type: Default 1000ms 256MiB

Longest Consecutive Subsequence

Longest Consecutive Subsequence

Given an array of n integers, your task is to find the length of the longest subsequence (not necessarily contiguous in the original order) such that the elements can be rearranged to form a sequence of consecutive integers. Formally, you need to find the maximum length k for which there exists a set of integers {a, a+1, ..., a+k-1} that can be obtained by reordering a subset of the given array.

For example, consider the array [10, 3, 5, 1, 4, 2]. The longest such subsequence is [3, 5, 1, 4, 2] which can be rearranged to form [1, 2, 3, 4, 5] and has a length of 5.

Note: If there are no elements, the answer is 0.

inputFormat

The input is given via standard input (stdin). The first line contains an integer nn, indicating the number of elements in the array. The second line contains nn space-separated integers representing the array elements.

outputFormat

Output a single integer representing the length of the longest subsequence that can be rearranged to form a sequence of consecutive integers. The answer should be printed to standard output (stdout).## sample

6
10 3 5 1 4 2
5