#K57097. Longest Consecutive Subsequence
Longest Consecutive Subsequence
Longest Consecutive Subsequence
Given a list of unique integers, determine the length of the longest subsequence such that every two consecutive numbers in the subsequence differ by exactly 1. The subsequence is not required to follow the given order; you may rearrange the numbers as necessary.
Note: The input guarantees that each integer in the list is unique, and the difference condition is applied to consecutive numbers when they are sorted in increasing order.
A common approach to solving this problem is to use a hash set to identify the start of a potential consecutive sequence and then count the number of consecutive integers following it.
The task is to read multiple test cases from standard input and, for each test case, print the length of the longest consecutive subsequence.
inputFormat
The first line of input contains a single integer T, representing the number of test cases. Each test case is described in two lines:
- The first line contains an integer N (the number of elements in the array).
- The second line contains N space-separated integers.
All input is provided via standard input.
outputFormat
For each test case, output a single integer on its own line representing the length of the longest consecutive subsequence. All output should be written to standard output.
## sample2
5
10 9 4 5 3
6
5 6 7 8 9 10
3
6
</p>