Skip to content

Commit c0fb787

Browse files
committed
Solution as on 14-04-2022 07:00 am
1 parent ef0f279 commit c0fb787

File tree

2 files changed

+42
-20
lines changed

2 files changed

+42
-20
lines changed

1539. Kth Missing Positive Number. Kth Missing Positive Number. Kth Missing Positive Number.cpp

Lines changed: 0 additions & 20 deletions
This file was deleted.
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
1539. Kth Missing Positive Number
2+
3+
class Solution
4+
{
5+
public:
6+
int findKthPositive(vector<int> &arr, int k)
7+
{
8+
if (arr[0] > k)
9+
return k;
10+
11+
int num = k;
12+
13+
for (int i : arr)
14+
{
15+
if (i <= num)
16+
{
17+
num++;
18+
}
19+
else
20+
break;
21+
}
22+
return num;
23+
}
24+
};
25+
26+
class Solution
27+
{
28+
public:
29+
int findKthPositive(vector<int> &arr, int k)
30+
{
31+
int start = 0, end = arr.size();
32+
while (start < end)
33+
{
34+
int mid = start + (end - start) / 2;
35+
if (arr[mid] - mid - 1 < k)
36+
start = mid + 1;
37+
else
38+
end = mid;
39+
}
40+
return start + k;
41+
}
42+
};

0 commit comments

Comments
 (0)