#C13180. Longest Consecutive Sequence

    ID: 42690 Type: Default 1000ms 256MiB

Longest Consecutive Sequence

Longest Consecutive Sequence

Given an unsorted array of integers, find the length of the longest sequence of consecutive integers. A consecutive sequence is defined as a sequence of numbers \(n, n+1, n+2, \ldots, n+k\) where each element appears in the array. The array may contain duplicates and negative numbers.

Example:

Input: [100, 4, 200, 1, 3, 2]
Output: 4
Explanation: The longest consecutive sequence is [1, 2, 3, 4].

Your task is to implement an efficient algorithm with an expected time complexity of \(O(n)\) to solve this problem.

inputFormat

The input is read from standard input (stdin) and has the following format:

  • The first line contains an integer \(n\), representing the number of elements in the array.
  • The second line contains \(n\) space-separated integers.

If \(n = 0\), the second line might be absent.

outputFormat

Output a single integer to standard output (stdout) denoting the length of the longest consecutive sequence in the array.

## sample
6
100 4 200 1 3 2
4

</p>