#C9087. Longest Consecutive Subsequence

    ID: 53141 Type: Default 1000ms 256MiB

Longest Consecutive Subsequence

Longest Consecutive Subsequence

The problem asks you to find the length of the longest consecutive integer subsequence in an unsorted array. In other words, you need to determine the maximum length of a sequence of integers such that each successive number is exactly one more than its previous number.

For example, given the array [1, 9, 3, 10, 4, 20, 2], the longest consecutive subsequence is [1, 2, 3, 4] which has a length of 4.

The input is provided via standard input (stdin) and the output should be written to standard output (stdout). Ensure that your solution efficiently handles unsorted arrays and duplicate values.

inputFormat

The input consists of two lines. The first line contains a single integer N, representing the number of elements in the array. The second line contains N space-separated integers representing the elements of the array. If N is 0, there will be no second line.

outputFormat

Output a single integer representing the length of the longest consecutive subsequence found in the array.## sample

7
1 9 3 10 4 20 2
4