Kth Missing Positive Number

Given a sorted array of positive integers, return the k-th missing positive number. The array order allows a direct count of gaps, which is a partial-selection style search problem.

Input Format

arr = sorted array of positive integers, k = missing index

Output Format

the k-th missing positive integer

Constraints

  • 1 <= arr.length <= 1000; 1 <= arr[i] <= 1000; arr is strictly increasing; 1 <= k <= 1000

Examples

Example 1:

Input:

arr = [2,3,4,7,11]
k = 5

Output:

9

Explanation:

The missing positives are 1,5,6,8,9, so the fifth is 9.

Example 2:

Input:

arr = [1,2,3,4]
k = 2

Output:

6

Explanation:

The missing positives start at 5, so the second is 6.

Loading...
Kth Missing Positive Number