-
Notifications
You must be signed in to change notification settings - Fork 1
/
42-find-all-4-sum-nos.cpp
78 lines (71 loc) · 2.1 KB
/
42-find-all-4-sum-nos.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include <bits/stdc++.h>
using namespace std;
// } Driver Code Ends
// User function template for C++
class Solution{
public:
// arr[] : int input array of integers
// k : the quadruple sum required
vector<vector<int> > fourSum(vector<int> &nums, int target)
{
vector<vector<int>> res;
int n = nums.size();
if(n < 4)
return res;
sort(nums.begin(), nums.end());
for(int i = 0; i < n; ++i)
{
for(int j = i + 1; j < n; ++j)
{
int target_2 = target - nums[i] - nums[j];
int front = j + 1, back = n - 1;
while(front < back)
{
int two_sum = nums[front] + nums[back];
if(two_sum > target_2) back--;
else if(two_sum < target_2) front++;
else
{
vector<int> ans(4);
ans[0] = nums[i];
ans[1] = nums[j];
ans[2] = nums[front];
ans[3] = nums[back];
res.push_back(ans);
while(front < back && nums[front] == ans[2]){ ++front;}
while(front < back && nums[back] == ans[3]){--back;}
}
}
while(j + 1 < n && nums[j + 1] == nums[j]) ++j;
}
while(i + 1 < n && nums[i + 1] == nums[i]) ++i;
}
return res;
}
};
// { Driver Code Starts.
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;
} // } Driver Code Ends