#C10585. Longest Consecutive Subsequence
Longest Consecutive Subsequence
Longest Consecutive Subsequence
Given an integer n and an array of n integers, your task is to find the length of the longest subsequence such that for every two consecutive elements a and b in the subsequence, the difference satisfies \( b - a = 1 \). Note that the subsequence can be obtained after sorting the array, and duplicate values are allowed; however, the difference between identical numbers is 0 and does not meet the condition.
Example:
- For the input array
[1, 2, 3, 4, 5, 6]
, the longest subsequence is[1, 2, 3, 4, 5, 6]
with length 6. - For the input array
[10, 12, 11, 14, 13]
, the longest subsequence is[10, 11, 12, 13, 14]
with length 5. - For the input array
[20, 20, 20, 20]
, the longest subsequence is just[20]
with length 1.
Use appropriate methods to read from stdin
and write the answer to stdout
.
inputFormat
The first line contains a single integer \( n \) representing the number of elements in the array. The second line contains \( n \) space-separated integers.
outputFormat
Output a single integer representing the length of the longest subsequence where every two consecutive elements differ by exactly 1.
## sample6
1 2 3 4 5 6
6
</p>