fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. typedef long long int lli;
  5.  
  6. int main() {
  7.  
  8. lli n;
  9. cin>>n;
  10.  
  11. lli ar[n];
  12.  
  13. for(lli i=0;i<n;i++)
  14. {
  15. cin>>ar[i];
  16. }
  17.  
  18. sort(ar,ar+n);
  19.  
  20. lli q;
  21. cin>>q;
  22.  
  23. for(lli i=0;i<q;i++)
  24. {
  25. lli k;
  26. cin>>k;
  27.  
  28. lli a[k];
  29.  
  30. for(lli j=0;j<k;j++)
  31. cin>>a[j];
  32. int flag=-1;
  33.  
  34. //seaching each ingredient of array a in the given array of ingredients ie arr
  35. for(lli j=0;j<k;j++)
  36. {
  37. flag=-1;
  38.  
  39. lli l=0;
  40. lli r=n-1;
  41.  
  42. // implementing binary search
  43.  
  44. while(l<=r)
  45. {
  46. lli mid=l+(r-l)/2;
  47.  
  48. if(ar[mid]==a[j])
  49. {//element is found
  50. flag=1;
  51. break;
  52. }
  53. else if(l==r && ar[mid]!=a[j])
  54. {//check if the element is not present in the array
  55. flag=0;
  56. break;
  57. }
  58. else if(ar[mid]>a[j])
  59. {// search in the left half of the array
  60. r=mid-1;
  61. }
  62. else if(ar[mid]<a[j])
  63. {//search in the right half of the array
  64. l=mid+1;
  65. }
  66. }
  67.  
  68. if(flag==0)// if atleast on element is not present exit the search loop of all other dishes
  69. break;
  70. }
  71. if(flag==1)
  72. {
  73. cout<<"YES"<<endl;
  74. }
  75. else
  76. {
  77. cout<<"NO"<<endl;
  78. }
  79.  
  80. }
  81.  
  82. return 0;
  83. }
  84.  
Success #stdin #stdout 0s 15240KB
stdin
6
1 2 3 4 6 9
3 
4
1 3 4 2
3
1 2 12
2
6 4
stdout
YES
NO
YES