splash365

Binary Search and Variations

Jan 6th, 2021 (edited)
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.92 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define     fast            ios_base::sync_with_stdio(false);cin.tie(NULL)
  5. #define     read            freopen("input.txt","r",stdin)
  6. #define     write           freopen("output.txt","w",stdout)
  7.  
  8. int binary_search(int *arr, int low, int high, int target)
  9. {
  10.     while(low <= high)
  11.     {
  12.         int mid = low + (high - low) / 2;
  13.         if(arr[mid] == target)
  14.             return mid;
  15.         else if(arr[mid] < target)
  16.             low = mid + 1;
  17.         else
  18.             high = mid - 1;
  19.     }
  20.     return -1;
  21. }
  22.  
  23. int lower_bound(int *arr, int low, int high, int target)
  24. {
  25.     while(low <= high)
  26.     {
  27.         int mid = low + (high - low) / 2;
  28.         if(arr[mid] < target)
  29.             low = mid + 1;
  30.         else
  31.             high = mid - 1;
  32.     }
  33.     return low;
  34. }
  35.  
  36. int upper_bound(int *arr, int low, int high, int target)
  37. {
  38.     while(low <= high)
  39.     {
  40.         int mid = low + (high - low) / 2;
  41.         if(arr[mid] <= target)
  42.             low = mid + 1;
  43.         else
  44.             high = mid - 1;
  45.     }
  46.     return low;
  47. }
  48.  
  49. int first_occurence(int *arr, int low, int high, int target)
  50. {
  51.     while(low <= high)
  52.     {
  53.         int mid = low + (high - low) / 2;
  54.         if(arr[mid] < target)
  55.             low = mid + 1;
  56.         else
  57.             high = mid - 1;
  58.     }
  59.     if(arr[low] == target)
  60.         return low;
  61.     else
  62.         return -1;
  63. }
  64.  
  65. int last_occurence(int *arr, int low, int high, int target)
  66. {
  67.     while(low <= high)
  68.     {
  69.         int mid = low + (high - low) / 2;
  70.         if(arr[mid] <= target)
  71.             low = mid + 1;
  72.         else
  73.             high = mid - 1;
  74.     }
  75.     if(arr[high] == target)
  76.         return high;
  77.     else
  78.         return -1;
  79. }
  80.  
  81. int main()
  82. {
  83. #ifndef ONLINE_JUDGE
  84.     read;
  85.     write;
  86. #endif
  87.     fast;
  88.     int arr[] = {6, 6, 7, 9, 9, 9, 9, 10, 15, 17, 22};
  89.     int n = sizeof(arr) / sizeof(arr[0]);
  90.     int x = binary_search(arr, 0, n - 1, 9);
  91.     int lb = lower_bound(arr, 0, n - 1, 9);
  92.     int ub = upper_bound(arr, 0, n - 1, 9);
  93.     int fo = first_occurence(arr, 0, n - 1, 9);
  94.     int lo = last_occurence(arr, 0, n - 1, 9);
  95.     cout << x << ' ' << lb << ' ' << ub << ' ' << fo << ' ' << lo << endl;
  96.     return 0;
  97. }
  98.  
  99.  
  100.  
Add Comment
Please, Sign In to add comment