#K68007. Longest Consecutive Sequence

    ID: 32768 Type: Default 1000ms 256MiB

Longest Consecutive Sequence

Longest Consecutive Sequence

The task is to determine the length of the longest consecutive sequence in an unsorted array of integers. A consecutive sequence is defined as a set of integers that can be arranged in the form \(a, a+1, a+2, \dots, a+k\) for some integer \(a\) and non-negative integer \(k\). For example, the array [100, 4, 200, 1, 3, 2] has a longest consecutive sequence [1, 2, 3, 4] with a length of 4.

Your program should read input from stdin and print the result to stdout. The solution should implement an efficient method to solve this problem, taking care of duplicate entries and negative numbers as well.

inputFormat

The input is provided via stdin in the following format:

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

outputFormat

Output via stdout a single integer: the length of the longest sequence of consecutive integers that can be formed from the array.## sample

1
1
1

</p>