-
Notifications
You must be signed in to change notification settings - Fork 11
/
CS_5_TripletSum.cpp
46 lines (44 loc) · 1.35 KB
/
CS_5_TripletSum.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
#include <iostream>
#include <math.h>
#include <bits/stdc++.h>
using namespace std;
// TLE wala solution
int main()
{
vector<int> arr = {10, 5, 5, 5, 2};
int n = arr.size();
int tripletSum = 12;
set<vector<int>> visited;
vector<vector<int>> ans;
for (int i = 0; i < n - 2; i++)
{
for (int j = i + 1; j < n - 1; j++)
{
for (int k = j + 1; k < n; k++)
{
if (arr[i] + arr[j] + arr[k] == tripletSum)
{
vector<int> triplet;
triplet.push_back(arr[i]);
triplet.push_back(arr[j]);
triplet.push_back(arr[k]);
sort(triplet.begin(), triplet.end());
// Check if the triplet is already present in the set or not before pushing it into the set and the answer vector array (ans), if it is not present, then push it into the set and the answer vector array (ans)
if (visited.find(triplet) == visited.end())
{
ans.push_back(triplet);
visited.insert(triplet);
}
}
}
}
}
cout << "Answer: ";
for (auto &vector : ans)
{
for (auto &vectorElem : vector)
{
cout << vectorElem << ' ';
}
}
}