Kth Missing Positive Number - Problem
Find the Kth Missing Positive Integer

You're given a sorted array of positive integers with some numbers missing from the sequence. Your task is to find the kth missing positive integer that should have been in the natural sequence but isn't present in the array.

For example, if the array is [2, 3, 4, 7, 11], the missing positive integers are 1, 5, 6, 8, 9, 10, 12, ...

Goal: Return the kth missing positive integer efficiently.
Input: A sorted array of positive integers and an integer k
Output: The kth missing positive integer

Input & Output

example_1.py โ€” Basic Case
$ Input: arr = [2,3,4,7,11], k = 5
โ€บ Output: 9
๐Ÿ’ก Note: The missing positive integers are [1,5,6,8,9,10,12,...]. The 5th missing positive integer is 9.
example_2.py โ€” Small Array
$ Input: arr = [1,2,3,4], k = 2
โ€บ Output: 6
๐Ÿ’ก Note: The missing positive integers are [5,6,7,8,...]. The 2nd missing positive integer is 6.
example_3.py โ€” Array Starts High
$ Input: arr = [10,20,30], k = 1
โ€บ Output: 1
๐Ÿ’ก Note: The missing positive integers are [1,2,3,4,5,6,7,8,9,11,...]. The 1st missing positive integer is 1.

Constraints

  • 1 โ‰ค arr.length โ‰ค 1000
  • 1 โ‰ค arr[i] โ‰ค 1000
  • 1 โ‰ค k โ‰ค 1000
  • arr is sorted in strictly increasing order

Visualization

Tap to expand
๐ŸŽญ Theater Seat Assignment AnalogySeat Numbers: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 151Available2VIP3VIP4VIP5Available6Available7VIP8Available95th Available!10Available11VIPBinary Search Process:VIP Array: [2, 3, 4, 7, 11] โ†’ Find 5th available seatPosition 0: Seat 2 โ†’ Missing before: 2-0-1 = 1 < 5 (search right)Position 3: Seat 7 โ†’ Missing before: 7-3-1 = 3 < 5 (search right)Position 4: Seat 11 โ†’ Missing before: 11-4-1 = 6 โ‰ฅ 5 (search left)Result: Insert at position 4 โ†’ Answer = 4 + 5 = 9Why This Works:โ€ข Before seat 7 (position 3): seats 1,5,6 are available (3 total)โ€ข We need the 5th available seat, so 2 more after position 3โ€ข Next available seats after 7: 8,9,10,... โ†’ 5th overall is seat 9๐Ÿ’ก Key insight: arr[i] - i - 1 gives us missing count before position i
Understanding the Visualization
1
Identify Reserved Seats
The array represents VIP-reserved seats [2,3,4,7,11]
2
Count Available Seats
For each reserved seat, calculate how many regular seats are available before it
3
Binary Search
Use binary search to quickly find which section contains the kth available seat
4
Calculate Final Seat
Once we know the section, calculate the exact seat number
Key Takeaway
๐ŸŽฏ Key Insight: By calculating how many seats are missing before each VIP reservation, we can use binary search to jump directly to the section containing our answer, achieving O(log n) time complexity!
Asked in
Google 15 Amazon 12 Microsoft 8 Facebook 6
62.4K Views
Medium Frequency
~15 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen