-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImplement lower bound
47 lines (38 loc) · 1.21 KB
/
Implement lower bound
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
//Implement lower bound
import java.io.*;
import java.util.*;
class GFG {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int tc = Integer.parseInt(br.readLine().trim());
while(tc-- > 0) {
String[] str = br.readLine().trim().split(" ");
int[] a = new int[str.length];
for(int i = 0; i < str.length; i++) {
a[i] = Integer.parseInt(str[i]);
}
String[] nk = br.readLine().trim().split(" ");
int k = Integer.parseInt(nk[0]);
Solution sln = new Solution();
int ans = sln.lowerBound(a, k);
System.out.println(ans);
System.out.println("~");
}
}
}
class Solution {
int lowerBound(int[] arr, int target) {
int lo = 0, hi = arr.length - 1, res = arr.length;
while(lo <= hi) {
int mid = lo + (hi - lo) / 2;
if(arr[mid] >= target) {
res = mid;
hi = mid - 1;
}
else {
lo = mid + 1;
}
}
return res;
}
}