#K65542. Longest Consecutive Sequence
Longest Consecutive Sequence
Longest Consecutive Sequence
You are given an unsorted array of integers. Your task is to find the length of the longest consecutive elements sequence. The consecutive sequence can be formed by using elements from the array that follow each other without any gap. For example, for the array [100, 4, 200, 1, 3, 2], the longest consecutive sequence is [1, 2, 3, 4] and its length is 4.
Note: Your algorithm should run in O(n) complexity.
Input Format:
The first line contains an integer n
representing the number of elements. The second line contains n
space-separated integers representing the elements of the array.
Output Format:
Print a single integer representing the length of the longest consecutive elements sequence.
inputFormat
The input consists of two lines:
- The first line contains an integer
n
, the number of elements in the array. - The second line contains
n
space-separated integers.
outputFormat
Output a single integer that represents the length of the longest consecutive sequence found in the array.
## sample6
100 4 200 1 3 2
4