Skip to content

Commit a7e40af

Browse files
committed
added solution to kth missing positive number
1 parent dd277c0 commit a7e40af

File tree

3 files changed

+90
-0
lines changed

3 files changed

+90
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Kth missing positive number
2+
3+
| # | Difficulty | Tag(s) | Link |
4+
| --- | ---------- | ----------------------- | -------------------------------------------------------------------------- |
5+
| 42 | easy | Hash map, binary search | [View problem](https://leetcode.com/problems/kth-missing-positive-number/) |
6+
7+
## Approaches
8+
9+
- linear search using hash map
10+
- binary search
11+
12+
### Linear search
13+
14+
- use a hash map to store which numbers are present
15+
- loop through 1 until 2000
16+
- problem states that the largest possible number in the input array is 1000
17+
- and the largest possible value of k is 1000
18+
- so, in the worst case, the 1000th missing number will be 2000
19+
- whenever we encounter a missing number, we decrement `k`
20+
- when `k` becomes 0, we have found the kth missing number
21+
- O(m + n) time complexity
22+
- m is the size of the input array
23+
- n is 2000
24+
- O(m) space complexity
25+
26+
### Binary search
27+
28+
- this approach involves finding the count of missing numbers at an index, and comparing it with `k`
29+
- how many numbers are missing until the number at that index?
30+
- we need to find the first index at which `missingNumsCount >= k`
31+
- use two pointers to track the start and end positions of the current problem space
32+
- calculate middle index and find `missingNumsCount` at middle index
33+
- if `missingNumsCount < k`, then the kth missing number must be to the right of `middle`
34+
- if `missingNumsCount >= k && missingNumsCountJustBeforeMid >= k`, then the kth missing number must be to the left of `middle`
35+
- O(log n) time complexity
36+
- O(1) space complexity
37+
38+
![](./kth-missing-pos-number.png)
116 KB
Loading
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
Linear search using hash map
3+
4+
*/
5+
6+
function findKthPositive(arr, k) {
7+
const numsPresent = {};
8+
9+
for (let i = 0; i < arr.length; i++) {
10+
numsPresent[arr[i]] = true;
11+
}
12+
13+
for (let i = 1; i <= 2000; i++) {
14+
if (!numsPresent[i]) k--;
15+
if (k === 0) return i;
16+
}
17+
}
18+
19+
console.log(findKthPositive([2, 3, 4, 7, 11], 5));
20+
console.log(findKthPositive([1, 2, 3, 4], 2));
21+
console.log(findKthPositive([2, 3, 4], 1));
22+
console.log(findKthPositive([5, 6, 7, 8, 9], 9));
23+
24+
/*
25+
Binary search
26+
*/
27+
28+
function findKthPositiveV2(arr, k) {
29+
let start = 0;
30+
let end = arr.length - 1;
31+
32+
while (start <= end) {
33+
const midIndex = Math.floor((end - start) / 2) + start;
34+
const missingNumsCountAtMidIndex = arr[midIndex] - (midIndex + 1);
35+
const missingNumsCountJustBeforeMidIndex = arr[midIndex - 1] - (midIndex - 1 + 1);
36+
if (missingNumsCountAtMidIndex < k) {
37+
start = midIndex + 1;
38+
} else if (missingNumsCountAtMidIndex >= k && missingNumsCountJustBeforeMidIndex >= k) {
39+
end = midIndex - 1;
40+
} else {
41+
return arr[midIndex] - (missingNumsCountAtMidIndex - k + 1);
42+
}
43+
}
44+
45+
return arr.length + k;
46+
}
47+
48+
console.log('---------');
49+
console.log(findKthPositiveV2([2, 3, 4, 7, 11], 5));
50+
console.log(findKthPositiveV2([1, 2, 3, 4], 2));
51+
console.log(findKthPositiveV2([2, 3, 4], 1));
52+
console.log(findKthPositiveV2([5, 6, 7, 8, 9], 9));

0 commit comments

Comments
 (0)