Given an array arr
of positive integers sorted in a strictly increasing order, and an integer k
, return the k-th positive integer that is missing from this array.
Example 1:
Input: arr = [2,3,4,7,11], k = 5
Output: 9
Explanation: The missing positive integers are [1,5,6,8,9,10,12,13,...]. The 5th missing positive integer is 9.
Example 2:
Input: arr = [1,2,3,4], k = 2
Output: 6
Explanation: The missing positive integers are [5,6,7,...]. The 2nd missing positive integer is 6.
Constraints:
1 <= arr.length <= 1000
1 <= arr[i] <= 1000
1 <= k <= 1000
arr[i] < arr[j]
for 1 <= i < j <= arr.length
Could you solve this problem in less than O(n) complexity?
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 way to find the missing positive number involves checking each positive integer one by one. We start with 1, then 2, then 3, and so on, and see if it is present in the provided set of numbers. We keep going until we've found the 'k'th missing number.
Here's how the algorithm would work step-by-step:
def find_kth_missing_positive(numbers, k):
missing_numbers_found = 0
current_integer = 1
while missing_numbers_found < k:
# Check if the current integer is in the array
if current_integer not in numbers:
# If it's not present, increment the missing numbers found
missing_numbers_found += 1
# This is the Kth missing number
if missing_numbers_found == k:
return current_integer
current_integer += 1
return current_integer - 1 # Return the kth missing positive number
The goal is to find the k-th positive number missing from a given list. We avoid checking every single number by cleverly using binary search to quickly narrow down the possibilities to find where the missing numbers are located and jump right to the answer.
Here's how the algorithm would work step-by-step:
def findKthPositive(array_of_integers, k_value):
left_index = 0
right_index = len(array_of_integers)
while left_index < right_index:
pivot_index = left_index + (right_index - left_index) // 2
# Count missing numbers up to the current index.
missing_numbers = array_of_integers[pivot_index] - (pivot_index + 1)
if missing_numbers < k_value:
left_index = pivot_index + 1
else:
right_index = pivot_index
# We need to calculate the kth missing number.
# The kth missing number is k plus the number just before left_index.
if left_index == 0:
return k_value
else:
return array_of_integers[left_index - 1] + (k_value - (array_of_integers[left_index - 1] - left_index))
Case | How to Handle |
---|---|
Empty input array | If the input array is empty, the first k positive integers are missing, so return k. |
k is smaller than the first missing positive integer | If k is smaller than arr[0], then k is the kth missing positive integer, so return k. |
k is larger than the number of missing positive integers before the end of the array | Calculate the number of missing integers and determine the kth missing positive number by adding the difference to the last element. |
Array contains very large positive integers | The algorithm should be able to handle large integers within the language's integer limits; check for integer overflow if necessary. |
k is very large | The algorithm's performance might degrade with large k, consider using binary search or other optimization techniques to improve efficiency. |
All numbers from 1 to n are present in the array | If all numbers from 1 to n (where n is the length of the array) are present, then the kth missing positive number is n + k. |
Array contains duplicate numbers | Duplicates do not affect the count of missing positive integers, the algorithm still finds the kth missing positive integer correctly. |
Large input array size | Binary search approach provides logarithmic time complexity, addressing scaling issues with large input arrays. |