Last Updated: 26 Nov, 2020

Kth Missing Positive Number

Moderate
Asked in company
Athenahealth

Problem statement

You are given a strictly increasing array 'vec' and a positive integer 'k'.


Find the 'kth' positive integer missing from 'vec'.


Example :
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. 


Input Format :
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.


Output Format :
Print the 'kth' positive integer missing from 'vec'.


Note :
You don't need to print anything, it has already been taken care of. Just implement the given function.

Approaches

01 Approach

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 :

  1. Run a loop from 0 to ‘n’ - 2 (say, iterator ‘i’).
  2. If ‘vec[i] + 1’ is not equal to ‘vec[i + 1]’, that is, elements are not consecutive, save their difference in a variable (say, ‘difference’) and decrement by one, which will give the count of absent elements between the elements ‘vec[i]’ and ‘vec[i + 1]’.
    • If the difference is lesser than ‘k’, deduct this difference from ‘k’. It means the missing element is not present between these 2 elements and the missing element count between these two elements is deducted
    • Else if the difference is greater than or equal to ‘k’, it means the missing element exists between the elements ‘vec[i]’ and ‘vec[i + 1]’. So, ‘vec[i]’ + ‘k’ will give the ‘k-th’ missing contiguous element in the given sequence starting from the leftmost element of the array. Turn ‘flag’ to ‘true’.
  3. If ‘flag’ is true, return the answer calculated in step 2b.
  4. Else, the answer will be greater than ‘vec[n - 1]’, so return ‘vec[n - 1]’ + ’k’.

02 Approach

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 :

  1. Create 2 variables (say, ‘low’ and ‘high’) and initialize them to 0 and  ‘n’ - 1 respectively.
  2. While ‘low’ is less than ‘high’ perform the following steps :
    • Find ‘mid’ using (‘low’ +  ‘high’) / 2.
    • Calculate the total missing elements between till ‘mid’ using ‘vec[mid]’ - ('mid' + 1) and store it in the variable (say, ‘missing’).
    • If ’missing’ is less than ‘k’, this means we need to shift towards right. So, we update the value of ‘low’ to ‘mid + 1’.
    • Else, shift to left by updating ‘high’ to ‘mid-1’.

    3. Return low+k;