Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <algorithm>
- using namespace std;
- int main(){
- int n = 0;
- cout << "Size : ";
- cin >> n;
- int a[n] = {};
- cout << "Elements : ";
- for (int i = 0; i < n; ++i) cin >> a[i];
- int key = 0;
- cout << "Key : ";
- cin >> key;
- sort(a, a + n);
- int l = -1;
- int r = n;
- while (l < r - 1){
- int m = (l + r) / 2;
- if (key > a[m]) l = m;
- else r = m;
- }
- if (a[r] == key) cout << "First element's index in sorted array : " << r;
- else cout << "Array doesn't have such element";
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement