Skip to content

Instantly share code, notes, and snippets.

@snawaz
Created January 27, 2022 18:26
Show Gist options
  • Save snawaz/0cb1c4e826fbfcda19d4c14d62430660 to your computer and use it in GitHub Desktop.
Save snawaz/0cb1c4e826fbfcda19d4c14d62430660 to your computer and use it in GitHub Desktop.
// https://www.hackerrank.com/challenges/missing-numbers/problem
// this solution posted as comment on, to explain how O(n) solution is designed:
// = https://www.youtube.com/watch?v=N6r9Dh68Y3o
vector<int> missingNumbers(vector<int> arr, vector<int> brr) {
auto const base = brr.front();
int freqs[100] {0};
int actual_base = base;
for(auto const item : brr) {
auto const index = (100 - base + item) % 100;
++freqs[index];
actual_base = std::min(actual_base, item);
}
for(auto const item : arr) {
auto const index = (100 - base + item) % 100;
--freqs[index];
}
vector<int> result;
auto index = (100 - base + actual_base) % 100;
for(auto i = 0; i < 100; ++i) {
if(freqs[index]) {
result.push_back(actual_base);
}
index = (index + 1) % 100;
actual_base++;
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment