#K77082. Longest Subsequence with Maximum Difference 1
Longest Subsequence with Maximum Difference 1
Longest Subsequence with Maximum Difference 1
Given an array of integers, your task is to find the length of the longest subsequence such that the difference between the maximum and minimum elements in the subsequence is at most 1. In other words, if you select a subsequence \( S \) from the array, it should satisfy:
\( \max(S) - \min(S) \leq 1 \).
The elements in the subsequence do not need to be contiguous in the original array.
Example: For the array [1, 2, 2, 3, 1], one optimal subsequence is [1, 2, 2, 1] which has length 4.
inputFormat
The input begins with an integer \( T \) representing the number of test cases. Each test case is given in the following format:
- An integer \( n \) representing the number of elements in the array.
- \( n \) space-separated integers.
All input is read from stdin
.
outputFormat
For each test case, output a single line containing one integer: the length of the longest subsequence whose maximum and minimum difference is at most 1. The results should be printed to stdout
.
1
5
1 2 2 3 1
4
</p>