Given a sorted array of positive integers arr
and an integer k
, find the k-th positive integer that is missing from this array.
For example:
arr = [2, 3, 4, 7, 11], k = 5
The missing positive integers are 1, 5, 6, 8, 9, 10, 12, 13, ... The 5th missing positive integer is 9.arr = [1, 2, 3, 4], k = 2
The missing positive integers are 5, 6, 7, ... The 2nd missing positive integer is 6.arr = [5, 6, 7, 8, 9], k = 3
The missing positive integers are 1, 2, 3, 4,... The 3rd missing positive integer is 3.arr = [2,3,4,7,11], k = 1
. The missing positive integers are 1, 5, 6... The 1st missing integer is 1.Write a function that takes the sorted array arr
and the integer k
as input, and returns the k-th missing positive integer.
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute-force method for finding a missing number is straightforward. We essentially check every possible number until we find the missing one. We do this by making sure the expected difference between the numbers is what we need it to be.
Here's how the algorithm would work step-by-step:
def find_missing_element_brute_force(sorted_array):
expected_number = sorted_array[0]
for index in range(len(sorted_array)):
# Check if the current number matches the expected number
if sorted_array[index] != expected_number:
# Found the missing element
return expected_number
# Update the expected number based on the sorted nature of the array
expected_number = sorted_array[index] + 1
# All elements were consecutive, so the missing element is after the last
return expected_number
The most efficient way to find the missing number is to avoid checking each number individually. Instead, we leverage the fact that the numbers are sorted to quickly narrow down the possibilities using a divide and conquer strategy.
Here's how the algorithm would work step-by-step:
def find_missing_element(sorted_array, missing_element_difference):
left_index = 0
right_index = len(sorted_array) - 1
while left_index <= right_index:
middle_index = (left_index + right_index) // 2
# Check if the actual number is what we'd expect based on its position.
expected_number = sorted_array[0] + middle_index * missing_element_difference
if sorted_array[middle_index] > expected_number:
# Missing number is somewhere earlier in the list.
right_index = middle_index - 1
else:
# Missing number is somewhere later in the list.
left_index = middle_index + 1
# leftIndex now points to the index where the missing number should be.
if right_index == -1:
return sorted_array[0] - missing_element_difference
else:
return sorted_array[right_index] + missing_element_difference
Case | How to Handle |
---|---|
Empty input array | Return -1 immediately since there is no missing element. |
k is larger than the total number of missing elements | Return the last element of the array plus k since all elements are missing beyond the array's range. |
The missing element is at the beginning of the array | The algorithm should correctly identify the missing element using the difference between the actual and expected values at index 0. |
The missing element is at the end of the array | The algorithm should correctly identify the missing element when it is larger than the largest element of the array. |
Input array contains duplicate numbers | The algorithm assumes a sorted array of distinct integers, so duplicates should be handled as if distinct by binary search. |
Integer overflow when calculating the expected value | Use long data type for calculations that may exceed the maximum integer value to prevent overflow. |
Large array size with large k value impacting binary search efficiency | Binary search should still be O(log n), but assess potential performance impact for extremely large inputs, though unlikely for typical interview constraints. |
k is zero | If k is 0, the algorithm should return the first missing number, meaning the first number that is not in sequence in the array. |