-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKth_Missing_Positive_Number.cpp
60 lines (56 loc) · 1.28 KB
/
Kth_Missing_Positive_Number.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <iostream>
#include <vector>
using namespace std;
// Time Complexity :- O(log n)
class Solution {
public:
int findKthPositive(vector<int>& arr, int k) {
int low = 0;
int high = arr.size() - 1;
while(low <= high)
{
int mid = (low + high)/2;
if(arr[mid] - (mid + 1) < k)
{
low = mid + 1;
}
else high = mid -1;
}
return low + k;
}
};
// Time Complexity :- O(n);
// Easy Solution
class Solution {
public:
int findKthPositive(vector<int>& arr, int k) {
for(int i = 0; i < arr.size(); i++)
{
if(arr[i] <= k) k++;
else break;
}
return k;
}
};
class Solution {
public:
int findKthPositive(vector<int>& arr, int k) {
int j = 0;
int maxi = arr[arr.size() - 1];
int count = 0;
int n = 0;
for(int i = 1; i <= maxi; i++)
{
if(i == arr[j]){
j++;
}
else{
count++;
n = i;
}
if(count == k) return n;
}
if(count == 0) return maxi+k;
return (maxi - count) + k;
}
};