fork(1) download
  1. import java.util.*;
  2. import java.lang.*;
  3. import java.io.*;
  4.  
  5. class Main {
  6. public static boolean find(int search, int[] arr, int len) {
  7. boolean ret = false;
  8. int fst = 0;
  9. int lst = len - 1;
  10. while (fst <= lst) {
  11. int mid = (fst + lst) / 2;
  12. if (search == arr[mid]) {
  13. ret = true;
  14. break;
  15. } else if (search < arr[mid]) {
  16. lst = mid - 1;
  17. } else {
  18. fst = mid + 1;
  19. }
  20. }
  21. return ret;
  22. }
  23.  
  24. public static void main(String[] args) {
  25. Scanner scan = new Scanner(System.in);
  26. int len = scan.nextInt();
  27. int[] arr = new int[len];
  28. int i;
  29. for (i = 0; i < len; i++) {
  30. arr[i] = scan.nextInt();
  31. }
  32. int num = scan.nextInt();
  33. int search;
  34. for (i = 0; i < num; i++) {
  35. search = scan.nextInt();
  36. System.out.print((find(search, arr, len))? "YES\n" : "NO\n");
  37. }
  38. }
  39. }
Success #stdin #stdout 0.05s 711680KB
stdin
10
1 4 7 11 12 43 44 67 344 355
5
1 2 4 44 45
stdout
YES
NO
YES
YES
NO