Problem 209
Kth Missing Positive Number
Easy · 10 pts · Array, Binary Search
Problem
You are given a strictly increasing array of positive integers and a number K. Consider the positive integers 1, 2, 3, ... that do not appear in the array, in increasing order. Print the K-th of them. The answer can be larger than every element of the array. Before index i, the number of missing positives is A[i] - (i + 1). Binary search for the first index where this count reaches K; the answer is then K plus the number of array elements before that index.
Input
First line: integers N and K. Second line: N strictly increasing positive integers.
Output
Print the K-th missing positive integer.
Constraints
1 <= N <= 100000; 1 <= A[i] <= 1000000000; 1 <= K <= 1000000000
Example input
5 5 2 3 4 7 11
Example output
9
Explanation
The array is 2 3 4 7 11. The missing positive integers are 1, 5, 6, 8, 9, 10, 12, ... The 5th of them is 9.