BugForge
DSA

Longest Run of Consecutive IDs

Mediumhash set 00:00

Order IDs arrive unsorted, possibly with duplicates. Return the length of the longest run of consecutive integers present (e.g. 1, 2, 3, 4).

Must run in O(n).

Signature

longestRun(ids: int[]) → int
Example 1
ids = [100,4,200,1,3,2]
output = 4
1, 2, 3, 4 → length 4
Example 2
ids = [0,3,7,2,5,8,4,6,0,1]
output = 9
0..8 → 9

Constraints

  • 0 ≤ n ≤ 10^5
  • -10^9 ≤ ids[i] ≤ 10^9
Run checks the examples. Submit runs every hidden test (20, including large inputs that catch slow solutions).