Kth Missing Positive Number - Problem

Given an array arr of positive integers sorted in a strictly increasing order, and an integer k.

Return the kth positive integer that is missing from this array.

The missing positive integers are those that should exist in a sequence starting from 1 but are not present in the given array.

Input & Output

Example 1 — Basic Case
$ Input: arr = [2,3,4,7,11], k = 5
Output: 9
💡 Note: Missing positive integers are [1,5,6,8,9,10,11,...]. The 5th missing positive integer is 9.
Example 2 — Small Array
$ Input: arr = [1,2,3,4], k = 2
Output: 6
💡 Note: Missing positive integers are [5,6,7,8,...]. The 2nd missing positive integer is 6.
Example 3 — Missing from Start
$ Input: arr = [2], k = 1
Output: 1
💡 Note: The only missing positive integer before 2 is 1, which is the 1st missing.

Constraints

  • 1 ≤ arr.length ≤ 1000
  • 1 ≤ arr[i] ≤ 1000
  • 1 ≤ k ≤ 1000
  • arr[i] < arr[i + 1]

Visualization

Tap to expand
Kth Missing Positive Number ProblemGiven Array:234711k = 5Complete Sequence:1234567891011Missing: 1, 5, 6, 8, 9, 10, ...1st2nd4th5th ✓6th3rdAnswer: 9Red = Missing, Green = Present in array
Understanding the Visualization
1
Input Analysis
Sorted array [2,3,4,7,11] with k=5
2
Find Missing
Identify missing positive integers: 1,5,6,8,9...
3
Get Kth
Return the 5th missing number which is 9
Key Takeaway
🎯 Key Insight: Calculate missing count at each position using the formula missing = arr[i] - (i + 1) to efficiently find where the kth missing number falls
Asked in
Facebook 35 Amazon 28 Google 22
98.5K Views
Medium Frequency
~15 min Avg. Time
2.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