forked from ndb796/python-for-coding-test
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path5.java
52 lines (44 loc) Β· 1.63 KB
/
5.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
51
52
import java.util.*;
public class Main {
// μ΄μ§ νμ μμ€μ½λ ꡬν(λ°λ³΅λ¬Έ)
public static int binarySearch(int[] arr, int target, int start, int end) {
while (start <= end) {
int mid = (start + end) / 2;
// μ°Ύμ κ²½μ° μ€κ°μ μΈλ±μ€ λ°ν
if (arr[mid] == target) return mid;
// μ€κ°μ μ κ°λ³΄λ€ μ°Ύκ³ μ νλ κ°μ΄ μμ κ²½μ° μΌμͺ½ νμΈ
else if (arr[mid] > target) end = mid - 1;
// μ€κ°μ μ κ°λ³΄λ€ μ°Ύκ³ μ νλ κ°μ΄ ν° κ²½μ° μ€λ₯Έμͺ½ νμΈ
else start = mid + 1;
}
return -1;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// N(κ°κ²μ λΆν κ°μ)
int n = sc.nextInt();
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
}
// μ΄μ§ νμμ μννκΈ° μν΄ μ¬μ μ μ λ ¬ μν
Arrays.sort(arr);
// M(μλμ΄ νμΈ μμ²ν λΆν κ°μ)
int m = sc.nextInt();
int[] targets = new int[m];
for (int i = 0; i < m; i++) {
targets[i] = sc.nextInt();
}
// μλμ΄ νμΈ μμ²ν λΆν λ²νΈλ₯Ό νλμ© νμΈ
for (int i = 0; i < m; i++) {
// ν΄λΉ λΆνμ΄ μ‘΄μ¬νλμ§ νμΈ
int result = binarySearch(arr, targets[i], 0, n - 1);
if (result != -1) {
System.out.print("yes ");
}
else {
System.out.print("no ");
}
}
}
}