Kth Missing Positive Number

Easy

Topics
ArrayBinary Search

Given a strictly increasing array of positive integers nums and an integer target (= k), return the target-th positive integer that is missing from the array.

Example 1

20314273114
The 5th missing positive is 9.
Cells are the array values; the small numbers underneath are indices.
Input:  nums = [2,3,4,7,11], target = 5
Output: 9

Example 2

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

Constraints

  • 1 <= nums.length <= 1000
  • 1 <= nums[i] <= 1000
  • nums is strictly increasing.
  • 1 <= target <= 1000
Editorial — intuition, approach, complexity and pitfalls

Intuition

Walking the array and counting gaps is O(n) and entirely acceptable. The reason to do better is that the array is strictly increasing, which makes the number of missing values monotonic — and anything monotonic can be binary searched.

The key quantity: at index i, the count of missing positives before nums[i] is nums[i] - (i + 1). If no positives were missing, nums[i] would be exactly i+1; the excess is precisely how many were skipped.

That count never decreases as i grows, so we binary search for the first index where it reaches k. Everything left of that index has fewer than k missing, so the answer lies in the gap at that boundary — and since lo values of the array sit below it, the answer is k + lo.

Approach

  1. Binary search over indices with lo = 0, hi = len(nums).
  2. If nums[mid] - (mid + 1) < k, the k-th missing is further right: lo = mid + 1. Else hi = mid.
  3. Return k + lo.

hi starts at len(nums), not len(nums) - 1, because the answer may lie beyond the last element.

Dry Run

nums = [2,3,4,7,11], k = 5
missing counts: nums[i] - (i+1) = [1,1,1,3,6]

lo=0 hi=5   mid=2  count=1 < 5  -> lo=3
lo=3 hi=5   mid=4  count=6 >= 5 -> hi=4
lo=3 hi=4   mid=3  count=3 < 5  -> lo=4
lo == hi == 4, stop

answer k + lo = 5 + 4 = 9

check: missing positives are 1,5,6,8,9,10,...  the 5th is 9  ✓

Complexity

  • Time — O(log n): one binary search, constant work per probe. The linear scan is O(n + k).
  • Space — O(1).

Common Pitfalls

  • Setting hi = len(nums) - 1. When the answer lies past the last element — as it does for a large k — the search cannot reach it.
  • Getting the missing-count formula off by one. It is nums[i] - (i + 1), because index i would hold i+1 in a gapless array.
  • Mixing loop shapes: this one uses while lo < hi with hi = mid, which converges on the boundary. Pairing it with hi = mid - 1 loses the answer.
  • Returning nums[lo] - 1 or similar. The answer is k + lo, derived from how many array values sit below it.
  • Using <= in the comparison, which lands on the last index with that count rather than the first.

Full Solution — all languages

class Solution:
    def findKthPositive(self, nums, target):
        lo, hi = 0, len(nums)
        while lo < hi:
            mid = (lo + hi) // 2
            if nums[mid] - (mid + 1) < target:
                lo = mid + 1
            else:
                hi = mid
        return target + lo
Asked at Morgan Stanley
SensAI
Beta
Tap the mic to stop voice mode