-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHalfFind.java
More file actions
40 lines (34 loc) · 1 KB
/
Copy pathHalfFind.java
File metadata and controls
40 lines (34 loc) · 1 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
import java.util.Scanner;
public class HalfFind {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
}
while (sc.hasNextInt()) {
int x = sc.nextInt();
int res = findNumber(x, arr, 0, arr.length - 1);
System.out.print(res + " ");
}
}
public static int findNumber(int x, int[] arr, int i, int j) {
if (i > j) {
return -1;
}
int mid = (i + j) / 2;
if (arr[mid] > x) {
return findNumber(x, arr, i, mid - 1);
} else if (arr[mid] < x) {
return findNumber(x, arr, mid + 1, j);
} else {
if (mid == 0 || arr[mid - 1] != x) {
return mid + 1;
} else {
return findNumber(x, arr, i, mid - 1);
}
}
}
}