287. Longest Run of Consecutive Values
Print the length of the longest set of consecutive integers that all appear somewhere in the array. Their positions do not matter and duplicates count once.
100 4 200 1 3 2 -> 4 (1, 2, 3, 4)
Sorting gives an O(n log n) answer and is perfectly acceptable. The
interesting solution is O(n) using a set.
The trick is deciding where to start counting. Put everything in a set, and then only begin a run at a value whose predecessor is ABSENT. That makes each run walked exactly once, so the total work is linear even though there is a loop inside a loop.
Constraints - `1 ≤ n ≤ 200000` - `-1000000000 ≤ a[i] ≤ 1000000000`
Input
The first line contains an integer n.
The second line contains n integers.
Output
Print one integer, the length of the longest consecutive run.