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, mid_index, begin_index = 0, end_index = end - begin;
- while (end_index - begin_index > 1) {
- mid = *(begin + (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) == target ||
- *(begin + end_index - 1) == target);
- }
- int* ArrayInit() {
- int arr_length;
- std::cin >> arr_length;
- int* arr = new int[arr_length];
- for (int i = 0; i < arr_length; ++i) {
- std::cin >> arr[i];
- }
- return arr;
- }
- void Searching(int* arr) {
- int func_calls, begin, end, target;
- std::cin >> func_calls;
- 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";
- }
- }
- }
- int main() {
- int* arr = ArrayInit();
- Searching(arr);
- delete[] arr;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement