-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3Sum.cpp
More file actions
28 lines (27 loc) · 857 Bytes
/
3Sum.cpp
File metadata and controls
28 lines (27 loc) · 857 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
#include <bits/stdc++.h>
vector<vector<int>> findTriplets(vector<int> arr, int n, int K)
{
// Write your code here.
vector<vector<int>> ans;
sort(arr.begin(), arr.end());
for (int i = 0; i < arr.size() - 2; i++)
{
for (int j = i + 1; j < arr.size() - 1; j++)
{
for (int l = j + 1; l < arr.size(); l++)
{
if (arr[i] + arr[j] + arr[l] == K)
{
vector<int> temp = {arr[i], arr[j], arr[l]};
// temp.push_back(arr[i]);
// temp.push_back(arr[j]);
// temp.push_back(arr[l]);
ans.push_back(temp);
}
}
}
}
set<vector<int>> s(ans.begin(), ans.end());
ans.assign(s.begin(), s.end());
return ans;
}