Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- bool BinarySearch(const int* begin, const int* end, int target) {
- int mid;
- int begin_index;
- int end_index;
- int mid_index;
- begin_index = 0;
- end_index = end - begin;
- while (end_index - begin_index > 1) {
- mid = *(begin - 1 + (end_index + begin_index) / 2);
- mid_index = (end_index + begin_index) / 2;
- if (mid > target) {
- end_index = mid_index;
- } else if (mid < target) {
- begin_index = mid_index;
- } else if (mid == target) {
- return true;
- }
- }
- return (*(begin + begin_index - 1) == target ||
- *(begin + end_index - 1) == target);
- }
- int main() {
- int arr_length;
- std::cin >> arr_length;
- int* arr = new int[arr_length];
- int func_calls;
- for (int i = 0; i < arr_length; i++) {
- std::cin >> arr[i];
- }
- std::cin >> func_calls;
- int begin;
- int end;
- int target;
- for (int calls = 0; calls < func_calls; calls++) {
- std::cin >> begin;
- std::cin >> end;
- std::cin >> target;
- if (BinarySearch(&arr[begin], &arr[end], target)) {
- std::cout << "YES\n";
- } else {
- std::cout << "NO\n";
- }
- }
- delete[] arr;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement