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 Mathematical Formula Approach INPUT Array arr (sorted): 2 3 4 7 11 i=0 i=1 i=2 i=3 i=4 k = 5 Positive integers 1 to 12: 1 2 3 4 5 6 7 8 9 10 11 12 In array Missing Answer ALGORITHM STEPS 1 Count Missing Numbers At index i: missing = arr[i]-(i+1) 2 Binary Search Find position where missing >= k 3 Calculate at Each Index Track missing count per position 4 Apply Formula result = k + position Missing Count at Each Index: i=0: arr[0]=2, missing=2-1=1 i=1: arr[1]=3, missing=3-2=1 i=2: arr[2]=4, missing=4-3=1 i=3: arr[3]=7, missing=7-4=3 i=4: arr[4]=11, missing=11-5=6 At i=4: 6 >= k=5 (found!) FINAL RESULT Formula Application: At i=3: missing = 3 < k=5 At i=4: missing = 6 >= k=5 result = k + 4 = 5 + 4 = 9 Missing numbers in order: 1 1st 5 2nd 6 3rd 8 4th 9 5th Output: 9 OK - 9 is the 5th missing positive number Key Insight: At index i, the count of missing positive integers before arr[i] is: arr[i] - (i + 1). Using binary search, we find the position where missing count >= k, then calculate result = k + position. Time: O(log n) | Space: O(1) - Efficient mathematical approach without iterating through all numbers. TutorialsPoint - Kth Missing Positive Number | Mathematical Formula Approach
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