Advertisement
Guest User

Untitled

a guest
Feb 10th, 2016
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.67 KB | None | 0 0
  1. #include <iostream>
  2. #include<algorithm>
  3. using namespace std;
  4. int BinarySearch(int number,int x[],int n) {
  5.     int lower = 0, upper = n - 1;
  6.     int ans = -1;
  7.      while (lower <= upper) {
  8.         int mid = (upper + lower) / 2;
  9.  
  10.         if (x[mid] > number) {
  11.             upper = mid - 1;
  12.         }
  13.         else {
  14.             ans = mid;
  15.             lower = mid + 1;
  16.         }
  17.     }
  18.  
  19.     return ans+ 1;
  20. }
  21.  
  22. int main()
  23. {
  24.     int n,m;
  25.     cin>>n>>m;
  26.     int x[n],y[m];
  27.     for(int i=0; i<n; i++)
  28.     {
  29.         cin>>x[i];
  30.     }
  31.     sort(x,x+n);
  32.  
  33.     for(int i=0; i<m; i++)
  34.     {
  35.  
  36. cin>>y[i];
  37. cout<<BinarySearch(y[i],x,n)<<" ";
  38.     }
  39.  
  40.     return 0;
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement