Skip to content

Instantly share code, notes, and snippets.

@feis
Last active August 29, 2015 14:24
Show Gist options
  • Save feis/5fb3d820161a365add5a to your computer and use it in GitHub Desktop.
Save feis/5fb3d820161a365add5a to your computer and use it in GitHub Desktop.
#include <array>
#include <iostream>
#include <unordered_map>
int main() {
auto numbers = std::array<int, 4>{2, 7, 11, 15};
auto table = std::unordered_map<int, int>();
int target = 9;
for (int i = 0; i < numbers.size(); ++i) {
auto m = table.find(target - numbers[i]);
if (m != table.end()) {
std::cout <<"index1=" << m->second + 1 << ", index2=" << i + 1 << std::endl;
break;
}
table[numbers[i]] = i;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment