Suppose we have an array called nums with positive sorted strictly increasing values, and also have an integer k. We have to find the kth positive integer that is missing from this array.
So, if the input is like nums = [1,2,4,8,12], k = 6, then the output will be 10 because the missing numbers are [3,5,6,7,9,10,11], here the 6th term is 10.
To solve this, we will follow these steps −
nums := a new set from the elements present in nums
count := 0
num := 1
while count < k, do
if num is not in nums, then
count := count + 1
if count is same as k, then
return num
num := num + 1
return num
Example (Python)
Let us see the following implementation to get better understanding −
def solve(nums, k): nums = set(nums) count = 0 num = 1 while count < k: if num not in nums: count += 1 if count == k: return num num += 1 return num nums = [1,2,4,8,12] k = 6 print(solve(nums, k))
Input
[1,2,4,8,12], 6
Output
10