fork download
  1. #include<bits/stdc++.h>
  2. #include<vector>
  3. #include<iterator>
  4. #include<algorithm>
  5. using namespace std;
  6. int main ()
  7. {
  8. int n,i,s,s1;
  9. cin>>n;
  10. int a[n];
  11. for(i=0;i<n;i++)
  12. {
  13. cin>>a[i];
  14. }
  15. vector<int>v(a,a+n);
  16. vector<int>:: iterator it;
  17. vector<int>:: iterator it1;
  18.  
  19. //lower_bound
  20.  
  21. cout<<"enter number"<<endl;
  22. cin>>s;
  23. it=lower_bound(v.begin(),v.end(),s);
  24.  
  25. if(*it!=s){
  26. cout<<s<<" "<<"not found"<<endl;
  27. }
  28. else
  29. {
  30. cout<<*it<<" "<<"found"<<endl;
  31. cout<<distance(v.begin(),it)<<endl;
  32. }
  33.  
  34. //upper_bound
  35.  
  36. cout<<"enter number"<<endl;
  37. cin>>s1;
  38. it1=upper_bound(v.begin(),v.end(),s1);
  39. it1--;
  40.  
  41. if(*it1!=s1){
  42. cout<<s1<<" "<<"not found"<<endl;
  43. }
  44. else
  45. {
  46. cout<<*it1<<" "<<"found"<<endl;
  47. cout<<distance(v.begin(),it1)<<endl;
  48. }
  49.  
  50.  
  51. }
  52.  
  53.  
Success #stdin #stdout 0s 4324KB
stdin
5
1 3 4 5 6
3
5
stdout
enter number
3 found
1
enter number
5 found
3