#C971. Longest Consecutive Subsequence

    ID: 53833 Type: Default 1000ms 256MiB

Longest Consecutive Subsequence

Longest Consecutive Subsequence

Given a sequence of integers, your task is to find the length of the longest subsequence that consists of consecutive integers. The consecutive numbers do not need to be adjacent in the original sequence. For example, if the sequence is 2, 6, 1, 9, 4, 5, 3, the longest consecutive subsequence is 1, 2, 3, 4, 5, 6 with length 6.

The input will contain multiple datasets. Each dataset starts with a positive integer N indicating the number of elements, followed by N integers. A dataset starting with 0 indicates the end of input and should not be processed.

You are required to output the length of the longest subsequence of consecutive integers for each dataset on a separate line.

inputFormat

The input consists of multiple datasets. For each dataset:

  • The first line contains a positive integer N representing the number of elements in the dataset.
  • The following N lines (or numbers separated by whitespace) contain the integers of the sequence.

A line with a single 0 signals the end of input and should not be processed.

outputFormat

For each dataset, print the length of the longest subsequence of consecutive integers on a separate line.

## sample
7
2
6
1
9
4
5
3
0
6

</p>