-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.java
More file actions
53 lines (39 loc) · 1.14 KB
/
Solution.java
File metadata and controls
53 lines (39 loc) · 1.14 KB
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
import java.util.*;
import java.io.*;
public class Solution {
public static int lowerBound(int[] arr, int target) {
int left = 0;
int right = arr.length - 1;
while (left < right) {
int mid = (left + right) / 2;
if (arr[mid] < target) {
left = mid + 1;
} else {
right = mid;
}
}
return left;
}
public static int upperBound(int[] arr, int target) {
int left = 0;
int right = arr.length - 1;
while (left < right) {
int mid = (left + right) / 2;
if (arr[mid] <= target) {
left = mid + 1;
} else {
right = mid;
}
}
return left;
}
public static void main(String[] args) throws IOException {
int[] arr = {3, 3, 7, 5, 1, 3};
Arrays.sort(arr);
int target = 3;
int lower = lowerBound(arr, target);
int upper = upperBound(arr, target);
System.out.println("lower: " + lower);
System.out.println("upper: " + upper);
}
}