Skip to content

Instantly share code, notes, and snippets.

@tolinwei
Last active January 4, 2016 06:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tolinwei/8585552 to your computer and use it in GitHub Desktop.
Save tolinwei/8585552 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <vector>
#include <unordered_set>
using namespace std;
void two_sum(int array[], int sum)
{
unordered_set<int> us;
vector<int> v (array, array+7);
for (int i = 0; i < v.size(); i++) {
if (us.find(sum - array[i]) == us.end()) {
us.insert(array[i]);
} else {
cout << sum - array[i] << " and " << array[i] << endl;
}
}
return;
}
int main(int argc, char *argv[]) {
int array[] = {1, 4, 5, 7, 11, 16, 17};
int sum = 15;
two_sum(array, sum);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment