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 = 0;
- int begin_index = 0;
- int end_index = 0;
- int mid_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 = 0;
- 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 SearchRequests(const int* arr){
- int function_calls = 0;
- std::cin >> function_calls;
- int begin = 0;
- int end = 0;
- int target = 0;
- for (int calls = 0; calls < function_calls; ++calls) {
- std::cin >> begin >> end >> target;
- if (BinarySearch(&arr[begin], &arr[end], target)) {
- std::cout << "YES\n";
- } else {
- std::cout << "NO\n";
- }
- }
- }
- int main() {
- int* arr = ArrayInit();
- SearchRequests(arr);
- delete[] arr;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement