Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- int binary_search(int *a,int low,int hi,int qury)
- {
- int mid;
- while(low<=hi)
- {
- mid=(low+hi)/2;
- if(a[mid]==qury)
- return mid;
- else if(a[mid]<qury)
- hi=mid-1;
- else
- low=mid+1;
- }
- }
- int main()
- {
- int i,j,k,l,n,a[60000],b,c,hi,low,mid,x,q;
- cin>>n;
- for(i=0;i<n;i++)
- cin>>a[i];
- cin>>q;
- for(i=0;i<q;i++)
- {
- cin>>b;
- if(b<=a[0])
- cout<<"X"<<" "<<a[0]<<endl;
- else if(b>=a[n-1])
- cout<<a[n-1]<<" "<<"X"<<endl;
- else
- {
- hi=n;
- low=0;
- mid=binary_search(a,0,n,b);
- cout<<a[mid-1]<<" "<<a[mid+1]<<endl;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement