Problem 391699 · easy · Level 03 Linear Management & Searching

The Gap in the Ticket Stubs

binary search · arrays

An usher sold tickets numbered consecutively from start up to start + len(nums) and lays the collected stubs out in increasing order as nums. Exactly one stub never came back. Return its number.

Examples

Input:  start = 3, nums = [3, 4, 5, 7, 8]
Output: 6

Input:  start = 1, nums = [2, 3, 4]
Output: 1
Explanation: the range is 1..4 and 1 is absent.

Input:  start = 0, nums = [0, 1, 2]
Output: 3
Explanation: the range is 0..3; the last value is the one missing.

Constraints

  • 0 <= len(nums) <= 10**6
  • -10**9 <= start <= 10**9
  • Aim for O(log n) time. (Scanning for the gap still passes the tests; the point is to find it by looking at about 20 elements.)

Goals

  • Use the relationship between index and value to locate a gap
  • Handle the missing value being at either end of the range
Starting Python…