forked from BabesGotByte/Coding_SkillSet_Topicwise
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAnswer20.cpp
44 lines (36 loc) · 1.09 KB
/
Answer20.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
class Solution {
public:
vector<vector<int>> fourSum(vector<int>& nums, int target) {
sort(nums.begin(), nums.end());
set<vector<int>>ans;
int n = nums.size();
for(int i=0; i<n-1; i++)
{
for(int j=i+1; j<n ; j++)
{
int low = j+1;
int high = n-1;
while(low<high)
{
int sum = nums[i] + nums[j] + nums[low] + nums[high];
if(sum == target)
{
ans.insert({nums[i], nums[j] , nums[low] , nums[high]});
low++;
high--;
}
else if(sum<target)
{
low++;
}
else
{
high--;
}
}
}
}
vector<vector<int>>req(ans.begin(), ans.end());
return req;
}
};