Skip to content

Instantly share code, notes, and snippets.

@misterpoloy
Last active August 18, 2020 05:16
Show Gist options
  • Save misterpoloy/b08a02dc7ed4fe044a8199864eb9a82a to your computer and use it in GitHub Desktop.
Save misterpoloy/b08a02dc7ed4fe044a8199864eb9a82a to your computer and use it in GitHub Desktop.
Two Number Sum.cpp
#include <vector>
#include <unordered_set>
using namespace std;
// Repasado 2 veces
vector<int> twoNumberSum(vector<int> array, int targetSum) {
unordered_set<int> targets;
for (int i = 0; i < array.size(); i++) {
int y = targetSum - array[i];
if (targets.find(y) != targets.end()) {
return vector<int> { array[i], y };
} else {
targets.insert(array[i]);
}
}
return {};
}
@misterpoloy
Copy link
Author

misterpoloy commented Aug 18, 2020

2 number sums

Solution 1

Use the diffrerene from the target and your current element, use a hash function to check wether exist.

Solution 3

Use a left pointer and a right pointer, if lower increment left and if gigher decrement right.

Two number sum

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment