Kth Missing Positive Number solution explained

Kth Missing Positive Number gives a strictly increasing array of positive integers and asks for the k-th positive integer that is not in it. At index i the count of missing numbers below arr[i] is arr[i] - (i + 1), and that count never decreases, so binary search for the first index where it reaches k. If that index is lo, the answer is lo + k, found in O(log n) time and O(1) memory.

By Published 7 min read

The problem, restated

You get a sorted array of distinct positive integers and a positive integer k. Imagine counting 1, 2, 3 and so on, skipping every number that appears in the array. Return the k-th number you say. The answer may be larger than everything in the array.

Examples written for this page
ArraykAnswerMissing numbers in order
[3, 4, 7, 11]111, 2, 5, 6, 8, 9, 10, 12, ...
[3, 4, 7, 11]35The third missing number
[3, 4, 7, 11]812Past the end of the array
[1, 2, 3]25Nothing is missing until after 3
[5]441, 2, 3, 4 are all missing before 5
Examples written for this page. Scroll sideways on a small screen.

What the interviewer is testing

This is an easy-rated problem with a binary search hiding in it, and the interviewer wants to see whether you find the right thing to search on. Searching for a value does not help; searching on a derived count does. Good answers state the invariant ("missing(i) is non-decreasing"), pick a clear search boundary, and derive the final formula instead of fiddling with plus and minus ones until a test passes.

Follow-ups

  • The array has duplicates. missing(i) is no longer exact; deduplicate first or count distinct values below each index.
  • Answer many queries for different k. Each is O(log n) on the same array, so precompute nothing beyond the array itself.
  • Find the k-th missing number between two bounds. Shift the counting so the lower bound plays the role of 1.
  • The array is not sorted. Sort it first, O(n log n), or use a set and walk, O(n + k).

Brute force

Count upward from 1 with a pointer into the array. Every number that matches the pointer is skipped and advances the pointer; every other number is a missing one, and the k-th of those is the answer. That takes O(n + k) time, which is fine when k is small but not when k can be in the millions.

def kth_missing_walk(arr, k):
    i, x = 0, 0
    while k > 0:
        x += 1
        if i < len(arr) and arr[i] == x:
            i += 1
        else:
            k -= 1
    return x
Python: walk the integers, O(n + k).

A neat linear variant avoids walking every integer: scan the array, and while the current value is at most k, increase k by one, since that value took one of the first k slots. Stop at the first value larger than k. The final k is the answer, in O(n) time.

def kth_missing_shift(arr, k):
    for x in arr:
        if x <= k:
            k += 1
        else:
            break
    return k
Python: shift k past every array value that sits at or below it.

Binary search on the missing count

If nothing were missing, arr[i] would equal i + 1. So arr[i] - (i + 1) is exactly how many positive integers below arr[i] are absent from the array. Because the array is strictly increasing, this count never goes down as i grows. That monotone count is what you binary search.

missing(i) for [3, 4, 7, 11]
Index iarr[i]missing(i) = arr[i] - (i + 1)Missing numbers below arr[i]
0321, 2
1421, 2
2741, 2, 5, 6
31171, 2, 5, 6, 8, 9, 10
missing(i) for [3, 4, 7, 11]. Scroll sideways on a small screen.

Let lo be the first index with missing(lo) >= k, or n if no index qualifies. The k-th missing number is then below arr[lo] (or past the end), and the lo array values before it are all smaller than it. So among the integers from 1 up to the answer there are k missing ones and lo present ones, which makes the answer lo + k.

def kth_missing(arr, k):
    lo, hi = 0, len(arr)
    while lo < hi:
        mid = (lo + hi) // 2
        if arr[mid] - (mid + 1) >= k:
            hi = mid
        else:
            lo = mid + 1
    return lo + k
Python: lower-bound search on arr[i] - (i + 1) >= k.
function kthMissing(arr, k) {
  let lo = 0;
  let hi = arr.length;
  while (lo < hi) {
    const mid = (lo + hi) >> 1;
    if (arr[mid] - (mid + 1) >= k) {
      hi = mid;
    } else {
      lo = mid + 1;
    }
  }
  return lo + k;
}
JavaScript: the same lower bound.

Trace [3, 4, 7, 11] with k = 3. lo 0, hi 4: mid 2 has missing 4, which is at least 3, so hi = 2. mid 1 has missing 2, too small, so lo = 2. The loop ends with lo = 2, and the answer is 2 + 3 = 5. With k = 8, every missing count is below 8, lo ends at 4, and the answer is 12.

The half-open range [lo, hi) with hi starting at n is what lets the search return n when the answer lies past the end. Starting hi at n - 1 is a common source of an off-by-one that only shows up for large k.

Complexity

n array values
ApproachTimeExtra memory
Walk the integers with a pointerO(n + k)O(1)
Shift k past small valuesO(n)O(1)
Binary search on missing(i)O(log n)O(1)
n array values. Scroll sideways on a small screen.

Edge cases

  • Empty array: the answer is k.
  • The answer comes before the first value, like [5] with k = 2, giving 2 and lo = 0.
  • The answer comes after the last value, like [1, 2, 3] with k = 2, giving 5 and lo = n.
  • No gaps at all in the array: every missing count is 0, so the answer is n + k.
  • Large k with a short array: the binary search and shift versions stay fast; the walk does not.

Common mistakes

  • Returning arr[lo] - something instead of lo + k, and getting the end-of-array case wrong.
  • Using > k instead of >= k in the condition, which overshoots whenever some missing count equals k exactly.
  • Setting hi to n - 1, so the search can never say "past the end".
  • Searching for k in the array itself, which has nothing to do with the missing count.
  • Assuming the answer is always inside the array's range.

How to explain it out loud

Fictional example

Finding what to search on
Candidate
If nothing were missing, the value at index i would be i plus 1. So arr[i] minus (i plus 1) is the number of missing integers below arr[i], and since the array is strictly increasing, that count only goes up.
Candidate
I will binary search for the first index where that count reaches k. Call it lo. Then the answer has lo array values below it and k missing values up to and including it, so it equals lo plus k.
Candidate
I use a half-open range so lo can end at n when the answer is past the last element. That is O(log n) time and constant memory.

Search Insert Position is the plain lower-bound search this solution builds on. Sqrt(x) is another binary search over a derived condition rather than a stored value. Count of Smaller Numbers After Self and Top K Frequent Words both depend on counting things in order, the first with a tree and the second with a heap.

Practicing with Natively

The risk on this problem is not the idea but the boundary, so practice saying the invariant before you code. On Mac or Windows, Natively can read the problem from a screenshot during a practice session and suggest an approach to check yours against once you have had a go, and the transcript of the session shows whether you justified lo + k or just asserted it. Read about the AI interview assistant.

Questions people ask

How do you solve Kth Missing Positive Number?

Binary search for the first index lo where arr[lo] - (lo + 1) is at least k, using a half-open range so lo can equal n. The answer is lo + k.

Why is the answer lo + k?

Up to the answer there are k missing numbers and lo numbers from the array, all smaller than it, so the answer is their total.

Is there a simple linear solution?

Yes: for each value in order, if it is at most k, increase k by one; stop at the first larger value. The final k is the answer, in O(n) time.

What is the time complexity?

O(log n) time and O(1) memory with binary search, compared with O(n + k) for walking the integers.

Where to go next

Practice with the same assistant you would use on a call: it listens, transcribes and suggests answers from your own resume and the job description.

Facts on this page were last checked on . Product details come from the Natively app itself; if something here is out of date, tell us at hello@heynatively.com.

Keep reading

Put it on your own machine.

A live-conversation assistant that runs locally. Your conversations stay on your machine.

macOS 12+ · Windows 10/11

© 2026 Natively AI Private Limited

Source-available under the Natively Personal Use Source License v1.0 · IP geolocation by DB-IP