Guest User

BinarySearch

a guest
Oct 14th, 2016
593
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.39 KB | None | 0 0
  1. const int N = 100010;
  2. int arr[N];
  3. int n;
  4.  
  5. int leftBinarySearch(int x) {
  6.     int l = -1, r = n;
  7.     while (r - l > 1) {
  8.         int m = (l + r) / 2;
  9.         if (arr[m] < x) {
  10.             l = m;
  11.         } else {
  12.             r = m;
  13.         }
  14.     }
  15.     return r;
  16. }
  17.  
  18. int rightBinarySearch(int x) {
  19.     int l = -1, r = n;
  20.     while (r - l > 1) {
  21.         int m = (l + r) / 2;
  22.         if (arr[m] <= x) {
  23.             l = m;
  24.         } else {
  25.             r = m;
  26.         }
  27.     }
  28.     return l;
  29. }
Advertisement
Add Comment
Please, Sign In to add comment