-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1539.kth missing positive number
50 lines (49 loc) · 1.14 KB
/
1539.kth missing positive number
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
class Solution {
public int findKthPositive(int[] arr, int k) {
if(arr[arr.length-1]==arr.length){
return arr[arr.length-1]+k;
}
int ans=binary(arr,k);
if(ans==-1){
int value=arr[arr.length-1]-(arr.length);
int find=k-value;
return arr[arr.length-1]+find;
}
int c=0;
int a=arr[ans]-1;
int index=ans-1;
int val=arr[ans]-(ans+1);
int val2=arr[ans]-1;
while(true){
if(index>=0){
if(val2==arr[index]){
val2--;
index--;
continue;
}
}
if(val==k){
return val2;
}
val--;
val2--;
}
}
static int binary(int arr[],int k){
int l=0;
int r=arr.length-1;
int si=-1;
while(l<=r){
int mid=l+(r-l)/2;
int mid2=arr[mid]-(mid+1);
if(mid2<k){
l=mid+1;
}
else{
si=mid;
r=mid-1;
}
}
return si;
}
}