fork download
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4.  
  5. // Function to perform binary search on the sorted array
  6. bool binarySearch(const vector<int>& arr, int target) {
  7. int low = 0;
  8. int high = arr.size() - 1;
  9.  
  10. while (low <= high) {
  11. int mid = low + (high - low) / 2;
  12.  
  13. if (arr[mid] == target) {
  14. return true;
  15. }
  16. if (arr[mid] < target) {
  17. low = mid + 1;
  18. } else {
  19. high = mid - 1;
  20. }
  21. }
  22.  
  23. return false;
  24. }
  25.  
  26. int main() {
  27. int n, k;
  28. cin >> n >> k;
  29.  
  30. // Input the first array (sorted)
  31. vector<int> firstArray(n);
  32. for (int i = 0; i < n; ++i) {
  33. cin >> firstArray[i];
  34. }
  35.  
  36. // Input the second array (numbers to search for)
  37. vector<int> secondArray(k);
  38. for (int i = 0; i < k; ++i) {
  39. cin >> secondArray[i];
  40. }
  41.  
  42. // For each element in the second array, perform binary search on the first array
  43. for (int i = 0; i < k; ++i) {
  44. if (binarySearch(firstArray, secondArray[i])) {
  45. cout << "YES" << endl;
  46. } else {
  47. cout << "NO" << endl;
  48. }
  49. }
  50.  
  51. return 0;
  52. }
  53.  
Success #stdin #stdout 0.01s 5296KB
stdin
10 5
1 2 3 4 5 
6 7 8 9 10
-2 0 4 9 
12
stdout
NO
NO
YES
YES
NO