Skip to content

Commit

Permalink
Merge pull request #37 from AlgoLeadMe/11-mong3125
Browse files Browse the repository at this point in the history
11-mong3125
  • Loading branch information
mong3125 authored Apr 10, 2024
2 parents 95b4a96 + 08e3f47 commit 465cfdb
Showing 1 changed file with 44 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;

public class Main {

static int[] A;
static int[] targets;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(br.readLine());
A = new int[N];

StringTokenizer st = new StringTokenizer(br.readLine());
for (int i = 0; i < A.length; i++) {
A[i] = Integer.parseInt(st.nextToken());
}

int M = Integer.parseInt(br.readLine());
targets = new int[M];
st = new StringTokenizer(br.readLine());
for (int i = 0; i < targets.length; i++) {
targets[i] = Integer.parseInt(st.nextToken());
}

Arrays.sort(A);

for (int i = 0; i < targets.length; i++) {
int now = targets[i];
System.out.println(binary(0, A.length - 1, now) ? 1 : 0);
}
}

public static boolean binary(int from, int to, int n) {
if (from > to) return false;
int mid = (from + to) / 2;

if (A[mid] == n) return true;
else if (A[mid] > n) return binary(from, mid - 1, n);
else return binary(mid + 1, to, n);
}
}

0 comments on commit 465cfdb

Please sign in to comment.