Question Name:Discover the Monk

#include<bits/stdc++.h>
using namespace std;
#define ll long long int
 
 
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ll n,q;
    cin>>n>>q;
    ll a[n];
    for(ll i=0;i<n;i++)
    cin>>a[i];
    sort(a,a+n);
    while(q--)
    {
        ll key;
        cin>>key;
        ll low=0;
        ll high =n-1;
        ll flag=0;
        while(low<=high)
        {
           ll mid = (low + high) /2;
           if(a[mid]<key)
           {
             low=mid+1;
           }
           else if(a[mid]>key)
           {
              high=mid-1;
           }
          else
            { flag=1;
            break;
        }
        }
        if(flag==1)
        cout<<"YES"<<endl;
        else
        cout<<"NO"<<endl;
         
    }
    return 0;
}
  • Problem Description
    You are given an array A of size N, and Q queries to deal with. For each query, you are given an integer X, and you’re supposed to find out if X is present in the array A or not.

    Input:
    The first line contains two integers, N and Q, denoting the size of array A and number of queries. The second line contains N space separated integers, denoting the array of elements Ai. The next Q lines contain a single integer X per line.

    Output:
    For each query, print YES if the X is in the array, otherwise print NO.

    Constraints:
    1 <= N, Q <= 10^5
    1 <= Ai <= 10^9
    1 <= X <= 10^9
  • Test Case 1
    Input (stdin)4 2
    20 30 40 10
    10
    20
    30
    40
    Expected OutputYES
    YES
  • Test Case 2
    Input (stdin)5 10
    50 40 30 20 10
    10
    20
    30
    40
    50
    60
    70
    80
    90
    100
    Expected OutputYES
    YES
    YES
    YES
    YES
    NO
    NO
    NO
    NO
    NO

Leave a Reply

Your email address will not be published. Required fields are marked *

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.