To solve the problem of finding the kth missing positive number, we need to understand where and how numbers go missing in the given array.
Understanding the Missing Numbers
Suppose we have an array like [2, 3, 4, 7]
. Normally, if no numbers were missing, we would expect the first 4 numbers to be [1, 2, 3, 4]
. But here, 1 is missing at the beginning. Then after 4, we expect 5 and 6, but we jump directly to 7, so 5 and 6 are missing too. This way, we can count how many numbers are missing up to any point.
Different Scenarios to Consider
- Missing numbers at the beginning: If the array starts from a number greater than 1, then everything before that number is missing. For example, in
[4, 5]
, the missing numbers are [1, 2, 3].
- Missing in the middle: If numbers jump from one to a much bigger next number, then there are several missing numbers in between.
- Empty array: If no numbers are given at all, then we assume all numbers are missing. So the kth missing is just
k
itself.
- All initial values are continuous: If the array starts at 1 and continues in sequence (e.g., [1, 2, 3, 4]), then missing numbers only start after the last element.
How We Find the Kth Missing
As we go through the array, we can check how many numbers are missing up to each index. For example, at index i
, the expected value is i + 1
, but the actual value is arr[i]
. So the number of missing values up to this point is arr[i] - (i + 1)
.
We continue this process until the number of missing values becomes at least k
. If that happens, we know the kth
missing value must be before or at that point.
If we reach the end of the array and still haven't found enough missing numbers, it means the missing number lies beyond the last element. In that case, the answer is arr.length + k
if we’ve found no missing elements, or more accurately, last array value + (k - total missing)
.
Efficiency
This can be solved efficiently using binary search because the missing count increases with the index. We search for the first position where the number of missing elements becomes at least k
and calculate the answer accordingly.
This approach ensures we avoid scanning all numbers one-by-one and still get the correct answer even for large arrays.