Difficulty | Source | Tags | ||
---|---|---|---|---|
Medium |
160 Days of Problem Solving |
|
The problem can be found at the following link: Question Link
Given a sorted array of distinct positive integers arr[]
and an integer k
, find the k
th positive number that is missing from the array.
Input:
arr = [2, 3, 4, 7, 11]
k = 5
Output:
9
Explanation:
The missing numbers are 1, 5, 6, 8, 9, 10, ...
. The 5th
missing number is 9
.
Input:
arr = [3, 5, 9, 10, 11, 12]
k = 2
Output:
2
Explanation:
The missing numbers are 1, 2, 4, 6, 7...
. The 2nd
missing number is 2
.
$1 <= arr.size() <= 10^5$ $1 <= k <= 10^5$ $1 <= arr[i]<= 10^6$
-
Key Observations:
- For an index
i
inarr
, the number of missing positive integers up toarr[i]
is given by:$[ \text{Missing Numbers} = arr[i] - (i + 1) $ ] - If this count is less than
k
, thek
th missing number lies afterarr[i]
. Otherwise, it lies beforearr[i]
.
- For an index
-
Steps:
- Use binary search over the array to find the smallest index
i
such that the count of missing numbers is at leastk
. - Once located, calculate the
k
th missing number using:$[ \text{Result} = \text{Index} + k ]$
- Use binary search over the array to find the smallest index
-
Edge Case:
- If all missing numbers are after the largest element in
arr
, return:$[ arr[-1] + (k - \text{Missing Numbers till end}) ]$
- If all missing numbers are after the largest element in
Expected Time Complexity:
where n
is the size of the array, as binary search is used to locate the position.
Expected Auxiliary Space Complexity:
since no additional data structures are used apart from a few variables.
int kthMissing(int* arr, int n, int k) {
int lo = 0, hi = n;
while (lo < hi) {
int mid = lo + (hi - lo) / 2;
if (arr[mid] - (mid + 1) < k) {
lo = mid + 1;
} else {
hi = mid;
}
}
return lo + k;
}
class Solution {
public:
int kthMissing(vector<int>& arr, int k) {
int lo = 0, hi = arr.size();
while (lo < hi) {
int mid = lo + (hi - lo) / 2;
if (arr[mid] - (mid + 1) < k)
lo = mid + 1;
else
hi = mid;
}
return lo + k;
}
};
class Solution {
public int kthMissing(int[] arr, int k) {
int lo = 0, hi = arr.length;
while (lo < hi) {
int mid = lo + (hi - lo) / 2;
if (arr[mid] - (mid + 1) < k) {
lo = mid + 1;
} else {
hi = mid;
}
}
return lo + k;
}
}
class Solution:
def kthMissing(self, arr, k):
lo, hi = 0, len(arr)
while lo < hi:
mid = lo + (hi - lo) // 2
if arr[mid] - (mid + 1) < k:
lo = mid + 1
else:
hi = mid
return lo + k
For discussions, questions, or doubts related to this solution, please visit my LinkedIn:- Any Questions. Thank you for your input; together, we strive to create a space where learning is a collaborative endeavor.
⭐ Star this repository if you find it helpful or intriguing! ⭐