+ 0 comments this code will fail the test cases 11 and 12. I know it that because of "if (x == *it)" this line. But I can't understand Why it is.
#include <cmath> #include <cstdio> #include <vector> #include <iostream> #include <set> #include <algorithm> using namespace std; int main() { long long q,y,x; cin >> q; set<long long>s; set<long long>::iterator it; for (long long i = 0; i < q; i++){ cin >> y >> x; if (y == 1){ s.insert(x); } else if (y == 2){ s.erase(x); } else { it = s.find(x); if (x == *it){ cout << "Yes"<< endl; } else{ cout << "No" << endl; } } } return 0; } >
+ 0 comments Although I pass all the tests,I still find certain situation that might be wrong using find function. What if the element isn't in the set,the find function will eventually return an iterator to the end,but if the element is just at the end of the set,the find function also will return an iterator to the end.There is no way to distinguish between the two cases using the find() function alone.
+ 0 comments Here are the solution of HackerRank Sets-STL in C++ Solution
Join Telegram Group for Updates Click Here
+ 0 comments Here is a C++ program that should solve the problem:
include
include
int main() { int q; std::cin >> q; std::set s; for (int i = 0; i < q; i++) { int y, x; std::cin >> y >> x; if (y == 1) { s.insert(x); } else if (y == 2) { s.erase(x); } else { std::cout << (s.count(x) ? "Yes" : "No") << std::endl; } } return 0; }
+ 2 comments I am trying to run the following code. It runs fine for the first test case. But on submitting compiler is giving this message: Abort called
int Q; cin>>Q; set<int> s; int x[Q],y[Q]; for(int i = 0 ; i < Q ;i++){ cin>>y[i]>>x[i]; } for(int i = 0 ; i< Q; i++){ if(y[i] == 1){ s.insert(x[i]); } else if(y[i] == 2){ set<int>::iterator itr=s.find(x[i]); s.erase(itr); } else if(y[i]==3){ set<int>::iterator itr=s.find(x[i]); if(itr == s.end()) cout<<"No"<<"\n"; else cout<<"Yes"<<"\n"; } } return 0;
Sort 297 Discussions, By:
Please Login in order to post a comment