fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int binarySearch(int arr[], int n, int x) {
  5. int left = 0, right = n - 1;
  6.  
  7. while (left <= right) {
  8. int mid = left + (right - left) / 2;
  9.  
  10. if (arr[mid] == x)
  11. return 1;
  12.  
  13. if (arr[mid] < x)
  14. left = mid + 1;
  15. else
  16. right = mid - 1;
  17. }
  18.  
  19. return -1;
  20. }
  21.  
  22. int main() {
  23. int n,target;
  24. cin >> n >> target;
  25. int a[n];
  26. for(int i = 0;i<n;i++){
  27. cin >> a[i];
  28. }
  29. int result = binarySearch(a, n, target);
  30. if(result==1) cout << "FOUND";
  31. else cout << "NOT FOUND";
  32.  
  33. return 0;
  34. }
  35.  
  36.  
Success #stdin #stdout 0.01s 5296KB
stdin
5
1 2 3 4 5
stdout
NOT FOUND