-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathKthMissingPositiveNumber.java
50 lines (41 loc) · 1.1 KB
/
KthMissingPositiveNumber.java
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
/*https://leetcode.com/problems/kth-missing-positive-number/*/
class Solution {
public int findKthPositive(int[] arr, int k) {
int index = 0, count = 0, number = 1, n = arr.length, prev = -1;
while (count < k)
{
if (index >= n || arr[index] > number)
{
prev = number;
++number;
++count;
}
else if (arr[index] == number)
{
prev = number;
++number;
++index;
}
}
return prev;
}
}
class Solution {
public int findKthPositive(int[] arr, int k) {
int start=0,end=arr.length-1;
if(k<arr[0]){
return k;
}
while(start<=end){
int mid = start + (end-start)/2;
int skippedNums = arr[mid]-(mid+1);
if(skippedNums<k){
start=mid+1;
}
else {
end=mid-1;
}
}
return start+k;
}
}