forked from Haresh1204/Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
4Sum.cpp
80 lines (67 loc) · 2.28 KB
/
4Sum.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
// Question-
// Suppose we have an array of numbers. It stores n integers, there are four elements a, b, c and d in the array. We have another target value, such that a + b + c + d = target. Find all unique quadruplets in the array which satisfies the situation. So if the array is like [-1,0,1,2,0,-2] and target is 0, then the result will be [[-1, 0, 0, 1],[-2, -1, 1, 2], [-2, 0, 0, 2]]
// Input-
// [1,0,-1,0,-2,2]
// 0
// Output-
// [[1,2,-1,-2],[0,2,0,-2],[0,1,0,-1]]
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
void addAll(vector < vector <int> >& res, vector < vector <int> >& temp){
for(int i = 0; i < temp.size(); i++)res.push_back(temp[i]);
}
vector<vector<int>> fourSum(vector<int>& nums, int target) {
sort(nums.begin(), nums.end());
return kSum(nums, 0, 4, target);
}
vector < vector <int> > kSum(vector <int>& arr, int start, int k, int target){
vector < vector <int> > res;
if(k == 2){
int left = start;
int right = arr.size() - 1;
vector <int> temp(2);
while(left < right){
if(arr[left] + arr[right] == target){
temp[0] = arr[left];
temp[1] = arr[right];
res.push_back(temp);
while(left < right && arr[left] == arr[left + 1])left++;
while(left < right && arr[right] == arr[right - 1])right--;
left++;
right--;
}
else if(arr[left] + arr[right] > target)right--;
else left ++;
}
}
else{
for(int i = start; i < (int)arr.size() - k + 1; i++){
if(i > start && arr[i] == arr[i - 1])continue;
vector < vector <int> > temp = kSum(arr, i + 1, k - 1, target - arr[i]);
for(int j = 0; j < temp.size(); j++){
temp[j].push_back(arr[i]);
}
addAll(res, temp);
}
}
return res;
}
};
void print_vector(vector<vector<int> > v){
cout << "[";
for(int i = 0; i<v.size(); i++){
cout << "[";
for(int j = 0; j <v[i].size(); j++){
cout << v[i][j] << ", ";
}
cout << "],";
}
cout << "]"<<endl;
}
void main(){
Solution ob;
vector<int> v = {1,0,-1,0,-2,2};
print_vector(ob.fourSum(v, 0));
}