Problem 367221 · medium · Phase 03 Linear Management & Searching

Kth Missing Positive

binary search · arrays

nums is a strictly increasing list of positive integers. Return the k-th positive integer that is not in nums.

Examples

Input:  nums = [2, 3, 4, 7, 11], k = 5
Output: 9
Explanation: the missing positives are 1, 5, 6, 8, 9, 10, 12, ...

Input:  nums = [1, 2, 3, 4], k = 2
Output: 6

Input:  nums = [5], k = 3
Output: 3

Constraints

  • 0 <= len(nums) <= 10**6, 1 <= k <= 10**9
  • 1 <= nums[i] <= 10**9
  • Required time: O(log n); walking through the missing numbers is too slow.

Goals

  • Derive a monotone quantity (missing count before index i) from the array
  • Handle the answer lying beyond the last element
Starting Python…