Loading...
Loading...
Loading...
Given n integers in any order, print the length of the longest group of values that form consecutive numbers (for example 4, 5, 6, 7). Repeated values count once. Searching the list for each next number is too slow.
Example
Input
6 100 4 200 1 3 2
Output
4
Try the problem first. Open one hint at a time.
For each value, asking x + 1 in numbers on a list repeats O(n) scans.
Put the values in a set. Start counting only at a value whose predecessor is not in the set, then walk upward.
Write your Python code, run it on the examples and submit it to hidden tests.
Solve in the editor