#1539

Kth Missing Positive Number

Easy Binary search on a derived value

  1. Read
  2. Understand
  3. Hints
  4. Approach
  5. Solution
  6. Solved

?The problem

Given a strictly increasing array of positive integers, return the k-th positive integer that is missing from it.

This is a short summary in our own words. Read the full statement and examples on LeetCode: open problem ↗

◎Understanding

Really asking: the k-th positive number that's not in the array.

Why this works: before index i, arr[i] - (i+1) numbers are missing, and that count only grows. Binary search the first index where it reaches k.

Example: [2, 3, 4, 7, 11], k = 5

missing counts: 1, 1, 1, 3, 6first index with count ≥ 5 is 4answer 4 + 5 = 9

✦Hints open one at a time

↗Approach

</>Solution (Java)

!Common mistake

Counting up one by one — O(n + k), fine for small inputs but misses the point.

Solve on LeetCode ↗
Complexity
  • Time: O(log n).
  • Space: O(1).
Topic: Binary Search
🎬 Episodes 21–23: video coming soon