Ctrl K

Longest Unbroken Run

Medium
You get an unordered integer array values. Find the largest k such that, for some x, all of x, x + 1, …, x + k - 1 appear in the array (anywhere in it). Return k. Repeated values count once. For an empty array the answer is 0. Sorting gives O(n log n); aim for O(n).
Example 1
values=[40, 7, 5, 41, 6, 8, -1] → 4

5, 6, 7 and 8 are all present, an unbroken run of length 4; 40 and 41 give only 2.

Example 2
values=[3, 3, 2, 2] → 2

Repeats do not extend a run: only 2 and 3 are there, so the answer is 2.

Example 3
values=[] → 0
Constraints
  • 0 ≤ len(values) ≤ 10⁵
  • -10⁹ ≤ values[i] ≤ 10⁹
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsMerge Contact Cards MediumChains of Synonyms MediumA Board Cut by Slashes MediumRearrange Through Links MediumEvery Rewording of a Sentence Medium
loading Python…
Input
values=[40, 7, 5, 41, 6, 8, -1]
Expected
4

Run checks the first 3 tests in your browser. Submit checks all 129 tests on the server, hidden ones included.