Skip to content

Instantly share code, notes, and snippets.

@valbok
Last active September 18, 2015 14:19
Show Gist options
  • Save valbok/e06df1997d540467b708 to your computer and use it in GitHub Desktop.
Save valbok/e06df1997d540467b708 to your computer and use it in GitHub Desktop.
#include <unordered_set>
#include <iostream>
template <class T, unsigned size>
bool findSum(T (&a)[size], T v, T& i1, T& i2) {
std::unordered_set<T> m;
for (unsigned i = 0; i < size; ++i) {
auto it = m.find(a[i]);
if (it == m.end()) {
m.insert(v - a[i]);
} else {
i1 = a[i];
i2 = v - *it;
return true;
}
}
return false;
}
int main() {
int A[] = {6,2,5,9};
int i1 = 0, i2 = 0;
int v = 11;
if (findSum(A, v, i1, i2)) {
std::cout << i1 << "+" << i2 << "=" << v << std::endl;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment