Ctrl K

The k-th Unsold Ticket

Easy
Lottery tickets are numbered with the positive integers 1, 2, 3, … with no upper limit. The list sold holds the numbers of tickets already sold, in strictly increasing order. Going through the numbers in ascending order, return the number of the k-th ticket that has not been sold. A straightforward scan is O(n + k); try to find the answer in less than linear time.
Example 1
sold=[1, 3, 4, 8], k=3 → 6

The free numbers are 2, 5, 6, 7, 9, … The third of them is 6.

Example 2
sold=[1, 2, 3], k=2 → 5

The first three numbers are taken in a row, so the free ones start at 4: 4, 5, … The second is 5.

Example 3
sold=[6, 9], k=4 → 4
Constraints
  • 1 ≤ len(sold) ≤ 10⁵
  • 1 ≤ sold[i] ≤ 10⁹
  • sold[i] < sold[i + 1]
  • 1 ≤ k ≤ 10⁹
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsSlot on a Sorted Shelf EasyThe Lost Ticket EasyValues in Both Lists EasyShared Values With Repeats EasyFind in a Sorted List Easy
loading Python…
Input
sold=[1, 3, 4, 8], k=3
Expected
6

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