Find the Kth Positive Integer Missing From a Sorted Array

Implement kthMissingPositive

Given a sorted array of distinct positive integers arr and an integer k, find the k-th positive integer that does not appear in the array. Every position in the array hides a count: at index i (0-indexed), if no positive integer were missing, arr[i] would equal i + 1 exactly. The gap between them — arr[i] - (i + 1) — is exactly how many positive integers are missing among the first i + 1 array slots. That gap only ever grows (or stays flat) moving rightward through the array, which makes this a binary search on the answerBinary Search on the AnswerThe search runs directly over the space of candidate answers rather than over the array's values. It applies whenever a derived quantity — here, the count of missing numbers so far — only ever increases (or only decreases) as the search position moves in one direction.: binary search finds the boundary index where the missing count first reaches k, and the answer follows directly from that boundary.

Example 1:

Input: arr = [4,5,6,9,13], k = 6

Output: 10

Example 2:

Input: arr = [2,3,4,5], k = 3

Output: 7

Example 3:

Input: arr = [6,7,8,11,14], k = 4

Output: 4

+ 5 hidden test cases run on Submit.

Constraints:

  • 1 ≤ length of arr ≤ 1000
  • arr is sorted in strictly increasing order
  • 1 ≤ arr[i] ≤ 1000
  • 1 ≤ k ≤ 1000

arr =

[4, 5, 6, 9, 13]

k =

6