Longest run of next numbers
Reported in interviews at (where)
Given an unsorted list of whole numbers nums, find the longest set of numbers in it that follow on from each other with no gap (like 4, 5, 6, 7), in whatever order they appear. Repeats count once.
Return how many numbers that run holds. Sorting first is O(n log n); aim for O(n).
- Input
nums = [9,1,4,7,3,2]- Output
4
1, 2, 3 and 4 are all there.
- Input
nums = [5,5,6]- Output
2
5 and 6; the second 5 adds nothing.
- Input
nums = []- Output
0
An empty list has no run.
Limits
0 <= nums.length <= 100000-10^9 <= nums[i] <= 10^9
Hints
Hint 1
Put every number in a set.
Hint 2
Only start counting at a number whose predecessor is missing: each run is then walked once.
Where this was reported
Our own problem on the task these candidates describe; the wording, examples and tests are InternDoor’s.
Run your code to check it on the examples. Submit runs every hidden test.
