Skip to content
BytePatterns

Kth Number Missing From a List

EasySearching#binary-search#counting~20m

Problem

Ticket numbers are handed out from 1 upwards, and nums lists the ones that have been used, strictly increasing and all positive. Return the k-th smallest positive number that does not appear in nums, where k is at least 1. Aim for a solution faster than walking through the numbers one by one.

Examples

Input:  nums = [2, 3, 4, 7, 11], k = 5
Output: 9
Why:    the missing numbers are 1, 5, 6, 8, 9, 10, ...
Input:  nums = [1, 2, 3, 4], k = 2
Output: 6
Why:    nothing is missing inside the list, so the answer lies past its end
Input:  nums = [], k = 4
Output: 4
Why:    edge case, with nothing used, the k-th missing number is k itself

Hints

0 / 3

Stuck on the idea rather than the code? Binary Search covers it.