Find the length of the longest run of consecutive integers among the numbers, in any order in the list. Repeated numbers count once.
Input
The first line has n (0 ≤ n ≤ 10⁵). The second line has the n integers (it is empty when n is 0).
Output
The length of the longest run (0 for an empty list).
Examples
Example 1 · Input
6 100 4 200 1 3 2
Expected output
4
Example 2 · Input
4 1 2 2 3
Expected output
3
Prompts inside input() are ignored when checking, so input("n: ") is fine.
Hints
Hint 1
Put the numbers in a set.
Hint 2
Only start counting at x when x − 1 is not in the set.
Hint 3
Count up while x + length is in the set.
Solution
Show one correct solution (try it yourself first)
n = int(input())
present = set(map(int, input().split()))
best = 0
for x in present:
if x - 1 not in present:
length = 1
while x + length in present:
length += 1
best = max(best, length)
print(best)