#C13145. Longest Consecutive Sequence

    ID: 42651 Type: Default 1000ms 256MiB

Longest Consecutive Sequence

Longest Consecutive Sequence

You are given an array of n integers. Your task is to determine the length of the longest consecutive sequence within this array. A consecutive sequence is defined as a set of integers where each number differs by exactly 1 from the previous number.

For example, consider the array [100, 4, 200, 1, 3, 2]. The longest consecutive sequence is [1, 2, 3, 4], so the answer is 4.

The required time complexity is \(O(n)\), where \(n\) is the number of elements in the array.

inputFormat

The input is provided via stdin in the following format:

 n
 a1 a2 ... an

Where:

  • n is a non-negative integer, representing the number of elements in the array.
  • ai are the integers in the array.

outputFormat

Output a single integer to stdout which is the length of the longest consecutive sequence found in the array.

## sample
6
100 4 200 1 3 2
4