#C10347. Longest Consecutive Subsequence
Longest Consecutive Subsequence
Longest Consecutive Subsequence
You are given an array of integers. Your task is to compute the length of the longest consecutive elements sequence. The consecutive sequence can be found in any order in the array, and the sequence does not need to be contiguous in the array. For example, given the input array [100, 4, 200, 1, 3, 2]
, the longest consecutive sequence is [1, 2, 3, 4]
and its length is 4.
Note: If the input array is empty, the output should be 0.
The solution should read the input from stdin and write the result to stdout in the following input format:
- The first line contains an integer n, representing the number of elements in the array.
- The second line contains n space-separated integers.
inputFormat
The first line of input is an integer n
(possibly 0), the number of elements in the array. The second line contains n
space-separated integers.
If n = 0
, there will be no second line.
outputFormat
Output a single integer which is the length of the longest consecutive subsequence in the array.
The answer should be printed to stdout.
## sample6
100 4 200 1 3 2
4