#K91597. Longest Consecutive Subsequence

    ID: 38010 Type: Default 1000ms 256MiB

Longest Consecutive Subsequence

Longest Consecutive Subsequence

Given an array of integers, your task is to determine the length of the longest consecutive subsequence. In a consecutive subsequence, each number differs from its preceding number by exactly 1. For example, in the array [1, 2, 3, 4, 5, 6] the longest consecutive subsequence is the entire array with a length of 6.

You should implement a solution that reads multiple test cases from stdin and prints the result for each test case to stdout.

Note: The difference between successive elements in the subsequence must exactly equal 1. The order of elements in the array does not matter since the solution can rearrange them using a set.

The key idea is to use a set to quickly check if an element is the start of a consecutive sequence (i.e. the element's predecessor is not in the array) and then count the length of the consecutive chain starting from that element.

The formula for the consecutive sequence starting at an element \(a\) is as follows: \[ length = \max_{a \in S,\, a-1 \notin S} \{1 + \sum_{k \ge 1} \mathbf{1}_{\{a+k \in S\}}\}\] where \(S\) is the set of all numbers in the array and \(\mathbf{1}_{\{\cdot\}}\) is the indicator function.

inputFormat

The input is read from stdin and has the following format:

T
N1
num1 num2 ... numN1
N2
num1 num2 ... numN2
... 
NT
num1 num2 ... numNT

Here, T is the number of test cases. For each test case, the first line contains an integer N representing the number of elements, followed by a line with N space-separated integers.

outputFormat

For each test case, output the length of the longest consecutive subsequence on a separate line to stdout.

## sample
1
6
1 2 3 4 5 6
6

</p>