Problem Overview

You are given a sorted array of distinct positive integers, arr[], and a positive integer k.

Your task is to find the kth missing positive number that does not appear in arr[].

Example 1

Input

Code
arr = [2, 3, 4, 7, 11]
k = 5

Output

Code
9

Explanation

Missing numbers:

Code
1, 5, 6, 8, 9, 10, ...

The 5th missing number is:

Code
9

Example 2

Input

Code
arr = [1, 2, 3]
k = 2

Output

Code
5

Explanation

Missing numbers:

Code
4, 5, 6, ...

The 2nd missing number is:

Code
5

Example 3

Input

Code
arr = [3, 5, 9, 10, 11, 12]
k = 2

Output

Code
2

Explanation

Missing numbers:

Code
1, 2, 4, 6, ...

The 2nd missing number is:

Code
2

Constraints

  • 1 ≤ arr.length ≤ 10^5

  • 1 ≤ k ≤ 10^5

  • 1 ≤ arr[i] ≤ 10^6

  • The array is sorted in ascending order.

  • All elements are distinct.

Understanding the Problem

We need to determine the kth positive number that is missing from the sorted array.

Example

Code
arr = [2, 3, 4, 7, 11]

The positive integers should ideally be:

Code
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ...

Numbers missing from the array are:

Code
1, 5, 6, 8, 9, 10, ...

Our goal is to find the kth number in this missing sequence.

Naive Approach

A straightforward solution would be:

  1. Start from 1.

  2. Check whether each number exists in the array.

  3. Count missing numbers.

  4. Stop when the kth missing number is found.

Complexity

Code
O(n + k)

This becomes inefficient when k is very large.

Since the array is already sorted, we can use binary search.

The key is to determine how many numbers are missing before a particular index.

Step 1: Count Missing Numbers Before an Index

At index i, if no numbers were missing, we would expect:

Code
arr[i] = i + 1

The actual value is larger because some numbers are missing.

Therefore:

Code
missing(i) = arr[i] - (i + 1)

This gives the number of missing positive integers before arr[i].

Example

Code
arr = [2, 3, 4, 7, 11]

At index 0:

Code
missing(0) = 2 - 1 = 1

At index 3:

Code
missing(3) = 7 - 4 = 3

At index 4:

Code
missing(4) = 11 - 5 = 6

Why Binary Search Works

We want to find the first position where:

Code
missing(i) ≥ k

Why?

Because the kth missing number must occur before that position.

This creates a monotonic property:

  • Smaller indices have fewer missing numbers.

  • Larger indices have more missing numbers.

That makes binary search applicable.

Binary Search Strategy

Maintain two pointers:

Code
left = 0
right = n - 1

For each midpoint:

Code
mid = left + (right - left) / 2

Compute:

Code
missing = arr[mid] - (mid + 1)

Case 1: Missing Numbers Are Less Than k

If:

Code
missing < k

the answer lies further right.

Code
left = mid + 1

Case 2: Missing Numbers Are Greater Than or Equal to k

If:

Code
missing ≥ k

the answer lies to the left.

Code
right = mid - 1

Computing the Final Answer

After binary search completes:

Code
left

represents the first position where:

Code
missing(left) ≥ k

The kth missing number is:

Code
left + k

Why Does left + k Work?

By the time we reach index left:

  • Exactly left array elements exist before that position.

  • We need to account for k missing numbers.

Combining both gives:

Code
kth missing = left + k

Example Walkthrough

Input

Code
arr = [2, 3, 4, 7, 11]
k = 5

Missing Counts

Code
missing(0) = 1
missing(1) = 1
missing(2) = 1
missing(3) = 3
missing(4) = 6

We need:

Code
missing(i) ≥ 5

The first such index is:

Code
left = 4

Therefore:

Code
kth missing = left + k
            = 4 + 5
            = 9

Answer

Code
9

Java Implementation

Java
class Solution {
    public int kthMissing(int[] arr, int k) {
        int n = arr.length;
        int left = 0, right = n - 1;

        while (left <= right) {
            int mid = left + (right - left) / 2;
            int missing = arr[mid] - (mid + 1);

            if (missing < k) {
                left = mid + 1;
            } else {
                right = mid - 1;
            }
        }

        // left is the position where missing(left) >= k
        return left + k;
    }
}

Complexity Analysis

Time Complexity

Binary search processes the array in:

Code
O(log n)

Space Complexity

Only a few variables are used:

Code
O(1)

Pattern Recognition

This problem belongs to the family of:

  • Binary Search on Answer

  • Missing Number Problems

  • Monotonic Search Space Problems

A common clue is when:

  • The array is sorted.

  • A counting function can be defined.

  • The count increases monotonically.

In such cases, binary search is often the optimal solution.

Key Takeaway

The crucial observation is that the number of missing elements before index i can be calculated as:

Code
arr[i] - (i + 1)

This creates a monotonic sequence that allows binary search. Instead of generating missing numbers one by one, we locate the first index where the missing count reaches k and directly compute the answer in logarithmic time.

Summary

To find the kth missing positive number efficiently, calculate how many numbers are missing before each array index using arr[i] - (i + 1). Since this count increases monotonically, binary search can be used to find the first index where the missing count becomes at least k. The final answer is then computed as left + k, resulting in an optimal O(log n) time and O(1) space solution.