In this HackerRank Sets-STL problem in the c++ programming language, you will be given Q queries. Each query is of one of the following three types:
- x: Add an element x to the set.
- x: Delete an element x from the set. (If the number x is not present in the set, then do nothing).
- x: If the number x is present in the set, then print “Yes”(without quotes) else print “No”(without quotes).
HackerRank Sets-STL problem solution in c++ programming.
#include <cmath> #include <cstdio> #include <vector> #include <iostream> #include <set> #include <algorithm> using namespace std; int main() { int iCount; set<int> ss; cin >> iCount; for (int i=0; i<iCount; ++i){ int type, query; cin >> type >> query; switch (type){ case 1: ss.insert(query); break; case 2: ss.erase(query); break; case 3: cout << (ss.find(query) == ss.end() ? "No" : "Yes") << endl; break; } } return 0; }
Second solution
#include <cmath> #include <cstdio> #include <vector> #include <iostream> #include <algorithm> #include <set> using namespace std; int main() { int c,t,n; scanf("%d",&n); set<int> ns; while(n--) { scanf("%d %d",&c,&t); switch(c) { case 1: ns.insert(t); break; case 2: ns.erase(t); break; case 3: if (ns.find(t)!=ns.end()) cout << "Yes"<<endl; else cout <<"No"<<endl; break; default: cout<<"invalid switch value: "<<c<<endl; } } /* Enter your code here. Read input from STDIN. Print output to STDOUT */ return 0; }