-
Notifications
You must be signed in to change notification settings - Fork 265
/
Copy pathTwoSum.cpp
42 lines (35 loc) · 1.08 KB
/
TwoSum.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>
#include <unordered_map>
#include <vector>
/*!
* \brief Determine whether in the list of retrieved numbers there is a
combination of two numbers that resulted in the target sum.
* \return Two indexes of the list whose sum of elements results in the
target value
*/
std::vector<int> twoSum(std::vector<int> &nums, int target) {
std::unordered_map<int, int> mapSum; // val, vect<idx>
for (int i = 0; i < nums.size(); i++) {
int compTarget = target - nums[i];
if (mapSum.count(compTarget)) {
return {i, mapSum[compTarget]};
} else {
mapSum.insert({nums[i], i});
}
}
return {};
}
int main() {
std::vector<int> values{9, 1, 2, 5, 7, 11, 15};
int target = 9;
auto sumIdxFor = twoSum(values, target);
if (sumIdxFor.empty()) {
std::cout << "no existent values with two sum: " << target << " in array"
<< std::endl;
} else {
std::cout << "Two sum for target: " << target << " in array is "
<< values[sumIdxFor[0]] << " and " << values[sumIdxFor[1]]
<< std::endl;
}
return 0;
}