Skip to content

Instantly share code, notes, and snippets.

@pascaljp
Created September 12, 2014 08:24
Show Gist options
  • Save pascaljp/d7895f2fd9e09cb55890 to your computer and use it in GitHub Desktop.
Save pascaljp/d7895f2fd9e09cb55890 to your computer and use it in GitHub Desktop.
#include <hash_map>
int solve(const vector<int>& A,
const vector<int>& B,
const vector<int>& C,
const vector<int>& D) {
int count = 0;
hash_map<int, int> AB = create_pairs(A, B);
hash_map<int, int> CD = create_pairs(C, D);
for (const auto& pair : AB) {
count += pair.second * CD[-pair.first];
}
return count;
}
hash_map<int, int> create_pairs(const vector<int>& S, const vector<int>& T) {
hash_map<int, int> pairs;
for (const int s : S) {
for (const int t : T) {
pairs[s + t]++;
}
}
return pairs;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment