
Input: vec = [2,4,5,7] , k = 3
Output: 6
Explanation :
In the given example, first missing positive integer is 1 second missing positive integer is 3, and the third missing positive integer is 6.
Hence the answer is 6.
The first line contains an integer ‘n’ denoting the number of elements in 'vec'.
The second line contains ‘n’ space-separated integers denoting the elements of 'vec'.
The third line contains an integer ‘k’ denoting the 'kth' missing element.
Print the 'kth' positive integer missing from 'vec'.
You don't need to print anything, it has already been taken care of. Just implement the given function.
First we remove the elements missing before the first number of the array which is trivial to check.
Then for each element check whether the current and next element is consecutive or not. If not, take the difference between the two and check till the difference is greater or equal to the given value of ‘k’. If the difference is greater, return current element - ‘count’.
Here is the algorithm :
At any index, we can check how many elements are missing till the element at that index. Now, using binary search we’ll find the closest index to the required answer.
Here is the algorithm :
3. Return low+k;