forked from coder2hacker/Explore-open-source
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path4 Sum.cpp
104 lines (49 loc) · 1.52 KB
/
4 Sum.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include<bits/stdc++.h>
using namespace std;
class Solution
{
public:
vector<vector<int>> fourSum(vector<int>& nums, int target)
{
int n = nums.size();
sort(nums.begin(),nums.end());
set<vector<int>> sv;
for(int i=0;i<n;i++)
{
for(int j=i+1;j<n;j++)
{
for(int k=j+1;k<n;k++)
{
int x = (long long)target -
(long long)nums[i]-
(long long)nums[j]-(long long)nums[k];
if(binary_search(nums.begin()+k+1,nums.end(),x))
{
vector<int> v;
v.push_back(nums[i]);
v.push_back(nums[j]);
v.push_back(nums[k]);
v.push_back(x);
sort(v.begin(),v.end());
sv.insert(v);
}
}
}
}
vector<vector<int>> res(sv.begin(),sv.end());
return res;
}
};
int main()
{
Solution obj;
vector<int> v{1,0,-1,0,-2,2};
vector<vector<int>> sum=obj.fourSum(v,0);
cout<<"The unique quadruplets are"<<endl;
for (int i = 0; i < sum.size(); i++)
{
for (int j = 0; j < sum[i].size(); j++)
cout << sum[i][j] << " ";
cout << endl;
}
}