Monk and his Friends

This is a question on HackerEarth [Monk and his Friends | Practice Problems]

MY logic is to create a unordered_multiset(for less time complexity) then using set.find() check whether the elements are present in the multiset or not. My solution is not giving TLE but is passing on just on test case…
I could not understand what is the problem or Did I understood question in the wrong way…
Please Help me…
This is my code…

indent preformatted text by 4 spaces// 

#include<bits/stdc++.h>
using namespace std;

void solve()
{
long long n,m;
cin>>n>>m;

unordered_multiset<long long> s;
vector<string> vec;
for(int i=0;i<n;i++)
{
    long long x;
    cin>>x;
    s.insert(x);
}
for(int i=n;i<n+m;i++) 
{
     long long x;
     cin>>x;
     if(s.find(x)!=s.end()) vec.push_back("YES");
     else vec.push_back("NO");
}
for(auto &p:vec) cout<<p<<"\n";
cout<<endl;

}

int main()
{
int t;
cin>>t;

while(t--)
{
    solve();
}

}

1 Like

Basically logic is we have to insert first n student’s marks in to map so that we could find similar pairs later, now when students from m coming into a class we’ve to check if there are similar marks present in the room or not, if yes then print else if not then insert that mark into the map for upcoming students.
Here is my code.

ll n,m,k,s; 
cin>>n>>m;
mpll mp; //map of long long int
for(int j=1;j<=(m+n);j++){
    cin>>k;
    if(j<=n){
        mp[k]++;
    }else if(j>n){
        if(mp[k]==0){
            cout<<"NO\n";
            mp[k]++;
        }else{
            cout<<"YES\n";
        }
    }
}

I think you’re not inserting those marks that are coming from m student, you’re just checking it if it is present or not.

1 Like

Yes, I Think you are right… thankyou for your help…

1 Like