Given an unsorted array A[] consisting of n integers, write a program to find the length of the longest consecutive sequence of integers in the array.

Problem Note

  • Your algorithm should run in O(n) complexity.
  • The consecutive numbers can be in any order.

Example 1

Input: A[] = [100, 4, 200, 1, 3, 2]
Output: 4
Explanation: The longest consecutive elements sequence is [1, 2, 3, 4]. Therefore its length is 4.


Example 2

Input: A[] = [20, 28, 12, 18, 23, 19]
Output: 3
Explanation: The longest consecutive elements sequence is [18, 19, 20]. Therefore its length is 3.