#K83342. Longest Continuous Subsequence

    ID: 36176 Type: Default 1000ms 256MiB

Longest Continuous Subsequence

Longest Continuous Subsequence

You are given a list of integers. Your task is to find the length of the longest subsequence (not necessarily contiguous in the original list) that can be rearranged to form a continuous sequence without any gaps. In other words, after reordering the selected numbers, they should form a sequence where every adjacent pair of numbers differs by exactly 1.

For example, consider the input array [100, 4, 200, 1, 3, 2]. The longest subsequence that can be rearranged into a continuous sequence is [1, 2, 3, 4], and hence the answer is 4.

Note: A continuous sequence is one where if the minimum element is \(a\) and the maximum is \(b\), then the sequence contains all integers from \(a\) to \(b\) (i.e., there are no missing numbers).

inputFormat

The input is read from standard input (stdin) and consists of two lines:

  • The first line contains an integer \(n\) denoting 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 which is the length of the longest subsequence that, after reordering, can form a continuous sequence.

## sample
6
100 4 200 1 3 2
4