Skip to content

Instantly share code, notes, and snippets.

@joonas-yoon
Created March 19, 2020 08:12
Show Gist options
  • Save joonas-yoon/7f1b10ff6254665de39b77ce4f99e255 to your computer and use it in GitHub Desktop.
Save joonas-yoon/7f1b10ff6254665de39b77ce4f99e255 to your computer and use it in GitHub Desktop.
alternative for STL - test/main
#include <cstdio>
// if you want to use STL, uncomment following includes:
// #include <vector>
// #include <algorithm>
// using namespace std;
// count "key" from array sized n
// if no key, print "NO"
int main() {
int n;
scanf("%d", &n);
vector<int> v(n);
for (int i = 0; i < v.size(); ++i) scanf("%d", &v[i]);
sort(v.begin(), v.end());
for (int i = 0; i < v.size(); ++i) printf("%d ", v[i]); puts("");
int key;
scanf("%d", &key);
if (binary_search(v.begin(), v.end(), key)) {
int equal_count = upper_bound(v.begin(), v.end(), key) - lower_bound(v.begin(), v.end(), key);
printf("%d\n", equal_count);
}
else {
puts("NO");
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment