Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <iostream>
- using namespace std;
- #define N 10000
- int mas[N];
- int BS(int l, int r, int key) {
- int mid = 0;
- if (l >= r) return -1;
- mid = (l + r) / 2;
- if (mas[mid] == mas[key]) return mid;
- if (mas[l] == mas[key]) return l;
- if (mas[r] == mas[key]) return r;
- if (mas[mid] < mas[key]) return BS(mid + 1, r, key);
- if (mas[mid] > mas[key]) return BS(l, mid - 1, key);
- return -1;
- }
- int main() {
- int n;
- cin >> n;
- for (int i = 0; i < n; i++) {
- cin >> mas[i];
- }
- cout << BS(0, n - 1, 3);
- return 0;
- }
Add Comment
Please, Sign In to add comment