-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfour_ele_equal_to_x.cpp
More file actions
34 lines (33 loc) · 1.13 KB
/
four_ele_equal_to_x.cpp
File metadata and controls
34 lines (33 loc) · 1.13 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
vector<vector<int> > fourSum(vector<int> &arr, int k) {
sort(arr.begin(), arr.end());
int n = arr.size();
vector<vector<int>> res;
for(int i=0; i<n-3; i++){
for(int j=i+1; j<n-2; j++){
int l = j+1;
int r = n - 1;
while(l < r){
if(arr[i] + arr[j] + arr[l] + arr[r] == k){
vector<int> values;
values.push_back(arr[i]);
values.push_back(arr[j]);
values.push_back(arr[l]);
values.push_back(arr[r]);
sort(values.begin(), values.end());
res.push_back(values);
l++;
r--;
}
else if(arr[i] + arr[j] + arr[l] + arr[r] < k){
l++;
}
else {
r--;
}
}
}
}
sort(res.begin(), res.end());
res.erase(unique(res.begin(), res.end()), res.end());
return res;
}