set and Membership
Completed
Read n integers into a set<int>, then q queries. Print the number of distinct values, the values in order, and yes/no for each query.
Input
n, the integers, q, then q queries.
Output
2 + q lines.
Example
Input
7 3 1 3 2 1 5 3 3 2 4 5
Output
4 1 2 3 5 yes no yes
Editor
C++
Hints
A set stores each value once, in sorted order.
count(x) is 1 if present, 0 otherwise.
Solution explanation
set lookups are O(log n).
Show solution
#include <iostream>
#include <set>
using namespace std;
int main() {
int n, q;
cin >> n;
set<int> s;
for (int i = 0; i < n; i++) {
int x;
cin >> x;
s.insert(x);
}
cout << s.size() << "\n";
bool first = true;
for (int x : s) {
cout << (first ? "" : " ") << x;
first = false;
}
cout << "\n";
cin >> q;
for (int i = 0; i < q; i++) {
int x;
cin >> x;
cout << (s.count(x) ? "yes" : "no") << "\n";
}
return 0;
}