Last active
September 18, 2015 14:19
-
-
Save valbok/e06df1997d540467b708 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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