Baseline Binary Search · In Search Space O(log n) · O(1)
A ticketing system allocates strictly increasing IDs, but outages left gaps in the issued sequence. Auditors reference issues by counting gaps - the k-th positive integer that was never issued. Find that value from the sorted list of issued IDs without walking every integer.
Input: An array arr of n strictly increasing positive integers and an integer k.
Output: The k-th positive integer missing from arr.
1 <= arr.length <= 10001 <= arr[i] <= 10001 <= k <= 1000arr is sorted in strictly increasing orderInput: {"arr":[2,3,4,7,11],"k":5}
Output: 9
The missing IDs are 1, 5, 6, 8, 9 in order, so the fifth gap is 9.
Input: {"arr":[1,2,3,4],"k":2}
Output: 6
Nothing is missing inside the range, so the gaps continue past the last ID: 5 then 6.