Advertisement
Farjana_akter

Untitled

Jan 28th, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.81 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int binary_search(int *a,int low,int hi,int qury)
  4. {
  5. int mid;
  6. while(low<=hi)
  7. {
  8. mid=(low+hi)/2;
  9. if(a[mid]==qury)
  10. return mid;
  11. else if(a[mid]<qury)
  12. hi=mid-1;
  13. else
  14. low=mid+1;
  15. }
  16.  
  17. }
  18. int main()
  19. {
  20. int i,j,k,l,n,a[60000],b,c,hi,low,mid,x,q;
  21. cin>>n;
  22. for(i=0;i<n;i++)
  23. cin>>a[i];
  24. cin>>q;
  25. for(i=0;i<q;i++)
  26. {
  27. cin>>b;
  28. if(b<=a[0])
  29. cout<<"X"<<" "<<a[0]<<endl;
  30. else if(b>=a[n-1])
  31. cout<<a[n-1]<<" "<<"X"<<endl;
  32. else
  33. {
  34. hi=n;
  35. low=0;
  36. mid=binary_search(a,0,n,b);
  37. cout<<a[mid-1]<<" "<<a[mid+1]<<endl;
  38.  
  39. }
  40. }
  41. return 0;
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement