Recent Post

Sets-STL.cpp-HackerRank

Problem- You will be given  queries. Each query is of one of the following three types:


  : Add an element  to the set.
  : Delete an element  from the set. (If the number  is not present in the set, then do nothing).
  : If the number  is present in the set, then print "Yes"(without quotes) else print "No"(without quotes).

Solution-
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <set>
#include <algorithm>
using namespace std;
int main()
{
    int q,i,t,x;
    set <int> s;
    cin>>q;
    for(i=0;i<q;i++)
    {
        cin>>t;
        if(t==1)
        {
            cin>>x;
            s.insert(x);
        }
        else if(t==2)
        {
            cin>>x;
            s.erase(x);
        }
            else
            {
                cin>>x;
                cout<<(s.find(x)==s.end()?"No":"Yes")<<endl;
            }
    }
}

Link to the problem-

Result!


Comments

Popular posts from this blog

Caesar Cipher.c-HackerRank

Bon Appétit.c-HackerRank

Electronics Shop.c-HackerRank