-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfourSum.cpp
More file actions
33 lines (29 loc) · 894 Bytes
/
fourSum.cpp
File metadata and controls
33 lines (29 loc) · 894 Bytes
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
vector<vector<int>> ans;
int n=arr.size();
int p,q;
sort(arr.begin(),arr.end());
for(int i=0;i<n-3;i++)
{
for(int j=i+1;j<n-2;j++)
{
p=j+1;
q=n-1;
while(p<q)
{
if(arr[i]+arr[j]+arr[p]+arr[q]==k)
{
vector<int>nums;
nums={arr[i],arr[j],arr[p],arr[q]};
ans.push_back(nums);
}
if(arr[i]+arr[j]+arr[p]+arr[q]<k)
p++;
else
q--;
}
}
}
sort(ans.begin(), ans.end());
// Use the unique algorithm to remove duplicates
ans.erase(unique(ans.begin(), ans.end()), ans.end());
return ans;