#C11557. Longest Consecutive Subsequence
Longest Consecutive Subsequence
Longest Consecutive Subsequence
You are given a list of n integers. Your task is to find the length of the longest chain of consecutive integers that can be formed using the numbers in the list. The integers in the chain must be consecutive, meaning that if you start with an integer a, the next integer must be a+1, followed by a+2, and so on.
Formally, given an array arr
of integers, determine the maximum length L such that there exists an integer x for which all numbers in the sequence
\[
x,\; x+1,\; x+2,\; \ldots,\; x+L-1
\]
are contained in arr
.
If the list is empty, output 0.
inputFormat
The first line contains an integer n
, denoting the number of elements in the list.
The second line contains n
space-separated integers.
outputFormat
Output a single integer representing the length of the longest chain of consecutive integers.
## sample5
1 2 3 4 5
5