-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFindAllFourSumNumbers.cpp
57 lines (55 loc) · 1.43 KB
/
FindAllFourSumNumbers.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <bits/stdc++.h>
using namespace std;
class Solution{
public:
vector<vector<int>> fourSum(vector<int> &arr, int k) {
int n = arr.size();
set<vector<int>> uniqueResult;
vector<vector<int>> result;
sort(arr.begin(), arr.end());
for (int i = 0; i < n - 3; i++) {
for (int j = i + 1; j < n - 2; j++) {
int l = j + 1, r = n - 1;
while(l < r) {
if(arr[i] + arr[j] + arr[l] + arr[r] == k) {
uniqueResult.insert({arr[i], arr[j], arr[l], arr[r]});
l++;
r--;
} else if(arr[i] + arr[j] + arr[l] + arr[r] < k)
l++;
else
r--;
}
}
}
for(auto it: uniqueResult) {
result.push_back(it);
}
return result;
}
};
int main() {
int t;
cin >> t;
while (t--) {
int n, k, i;
cin >> n >> k;
vector<int> a(n);
for (i = 0; i < n; i++) {
cin >> a[i];
}
Solution ob;
vector<vector<int>> ans = ob.fourSum(a, k);
for (auto &v : ans) {
for (int &u : v) {
cout << u << " ";
}
cout << "$";
}
if (ans.empty()) {
cout << -1;
}
cout << "\n";
}
return 0;
}