-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path4SUM.cpp
More file actions
62 lines (58 loc) · 1.4 KB
/
4SUM.cpp
File metadata and controls
62 lines (58 loc) · 1.4 KB
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
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define yes cout << "YES" << endl;
#define no cout << "NO" << endl;
#define input for(int &i:v) cin >> i;
#define sort(v) sort(v.begin(),v.end());
void print(const vector<int>& v) {
for (int i : v) {
cout << i << endl;
}
cout << endl;
}
void print(const vector<vector<int>>& v) {
for (vector<int> i : v) {
print(i);
}
cout << endl;
}
void fourSum(vector<int>& v, int target) {
//two loops
vector<vector<int>>ans;
sort(v);
//fix i and j, then two pointer on k and l
int n = v.size();
for(int i=0;i<n;i++){
for(int j=i+1;j<n;j++){
int cur = v[i]+v[j];
int need = target - cur;
int l = j+1;
int r = n-1;
while(l<r){
int second = v[l]+v[r];
if(need > second){
l++;
}
else if(need < second){
r--;
}
else{
ans.push_back({v[i],v[j],v[l],v[r]});
l++; r--;
}
}
}
}
print(ans);
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
int t=1;
// cin >> t;
while (t-- > 0) {
vector<int>v = {1,0,-1,0,-2,2};
fourSum(v,0);
}
}