Advertisement
Falak_Ahmed_Shakib

bainary such

Nov 26th, 2019
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.96 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. typedef long long ll;
  4. #define fastread() (ios_base:: sync_with_stdio(false),cin.tie(NULL))
  5. #define fi first
  6. #define se second
  7. #define pb push_back
  8. #define eb emplace_back
  9. void input(ll ara[],ll n)
  10. {
  11. for(ll i=0; i<n; i++)
  12. cin>>ara[i];
  13. }
  14.  
  15. #define eb emplace_back
  16. int main()
  17. {
  18. fastread();
  19.  
  20.  
  21.  
  22. ll n;
  23.  
  24. cin>>n;
  25.  
  26. ll ara[n+1];
  27.  
  28. input(ara,n);
  29.  
  30. sort(ara,ara+n);
  31. ll s=0,e=n-1;
  32. ll mid=(s+e)/2;
  33.  
  34. ll key;
  35. bool f=false;
  36. cin>>key;
  37.  
  38.  
  39. while(s<=e)
  40. {
  41.  
  42. cout<<mid<<endl;
  43. if(ara[mid]==key)
  44. {
  45. f=true;
  46. break;
  47. }
  48. else if(ara[mid]>key)
  49. {
  50. e=mid-1;
  51.  
  52. }
  53. else if(ara[mid]<key)
  54. {
  55. s=mid+1;
  56. }
  57. mid=(s+e)/2;
  58.  
  59.  
  60. }
  61. if(f==true)cout<<"YES"<<endl;
  62. else cout<<"NO"<<endl;
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73.  
  74.  
  75.  
  76.  
  77.  
  78.  
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement