-
Notifications
You must be signed in to change notification settings - Fork 110
Expand file tree
/
Copy pathfour-number-sum.cpp
More file actions
35 lines (32 loc) · 920 Bytes
/
four-number-sum.cpp
File metadata and controls
35 lines (32 loc) · 920 Bytes
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
#include <vector>
#include <unordered_map>
#include <algorithm>
using namespace std;
vector<vector<int> > fourNumberSum(vector<int> array, int targetSum) {
// Write your code here.
unordered_map<int, vector<vector<int>>> m;
vector<vector<int>> result;
for (int c1 = 0; c1 < array.size(); c1++) {
for (int c2 = c1+1; c2 < array.size(); c2++) {
int comp = targetSum - (array[c1] + array[c2]);
auto it = m.find(comp);
if (it != m.end()) {
vector<vector<int>> doubles = it->second;
for (vector<int> d : doubles) {
d.push_back(array[c1]); d.push_back(array[c2]);
result.push_back(d);
}
}
}
for (int c3 = 0; c3 < c1; c3++) {
int sum = array[c1] + array[c3];
auto it = m.find(sum);
if (it != m.end()) {
(it->second).push_back({array[c1], array[c3]});
} else {
m[sum] = {{array[c1], array[c3]}};
}
}
}
return result;
}