#K57232. Longest Subsequence with Consecutive Differences of One
Longest Subsequence with Consecutive Differences of One
Longest Subsequence with Consecutive Differences of One
You are given T test cases. For each test case, you are provided with an array of integers. Your task is to determine the length of the longest subsequence (after sorting the array) such that the absolute difference between any two consecutive elements is exactly 1.
Note: The subsequence is determined after sorting the array. In other words, find the longest chain of consecutive integers in the sorted array.
For example:
- For the array [1, 2, 3, 4, 5, 6], the longest such subsequence is the entire array with a length of 6.
- For [1, 3, 5, 7, 9, 11, 13], no consecutive numbers differ by 1, so the answer is 1.
- For [10, 9, 4, 5, 6], after sorting it becomes [4, 5, 6, 9, 10] and the longest consecutive chain is [4, 5, 6] with length 3.
inputFormat
The input is read from standard input (stdin) and has the following format:
T n1 a1 a2 ... an1 n2 b1 b2 ... bn2 ...
Here, T
is the number of test cases. For each test case, the first integer n
denotes the size of the array, followed by n
space-separated integers.
outputFormat
For each test case, output a single integer on a new line representing the length of the longest subsequence (after sorting) such that the absolute difference between any two consecutive elements is exactly 1.
## sample3
6 1 2 3 4 5 6
7 1 3 5 7 9 11 13
5 10 9 4 5 6
6
1
3
</p>