SHARE
TWEET

Untitled

Andoroid Dec 9th, 2019 83 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include<bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. int n;
  6. int a[100000];
  7.  
  8. int binarySearch(int l, int r, int x)
  9. {
  10.     if (r >= l) {
  11.         int mid = l + (r - l) / 2;
  12.  
  13.         if (a[mid] == x)
  14.             return mid;
  15.  
  16.         if (a[mid] > x)
  17.             return binarySearch(l, mid - 1, x);
  18.         return binarySearch(mid + 1, r, x);
  19.     }
  20.  
  21.     return -1;
  22. }
  23.  
  24. int main() {
  25.     n=20;
  26.     for(int i=0;i<n;i++) a[i] = rand()%50;
  27.     for(int i=0;i<n;i++) cout << a[i] << " ";
  28.     cout << "\n";
  29.     sort(a,a+n);
  30.     for(int i=0;i<n;i++) cout << a[i] << " ";
  31.     cout << "\n";
  32.     int c;
  33.     cin >> c;
  34.     int p = binarySearch(0,n-1,c);
  35.     if(p == -1) {
  36.         cout << "0" << "\n";
  37.     }
  38.     int startP = p;
  39.     while(a[p-1] == c && p>=1) p--;
  40.     cout << p+1 << " ";
  41.     p = startP;
  42.     while (a[p+1] == c && p < n-1) p++;
  43.     cout << p+1 << "\n";
  44. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top