forked from ArmanKumar21/python-cpp-html-programs-projects
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3Sum.cpp
More file actions
34 lines (29 loc) · 577 Bytes
/
3Sum.cpp
File metadata and controls
34 lines (29 loc) · 577 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
33
34
3Sum problem
// code
#include <bits/stdc++.h>
using namespace std;
void findTriplets(int arr[], int n)
{
bool found = false;
for (int i = 0; i < n - 1; i++) {
unordered_set<int> s;
for (int j = i + 1; j < n; j++) {
int x = -(arr[i] + arr[j]);
if (s.find(x) != s.end()) {
printf("%d %d %d\n", x, arr[i], arr[j]);
found = true;
}
else
s.insert(arr[j]);
}
}
if (found == false)
cout << " No Triplet Found" << endl;
}
int main()
{
int arr[] = { 0, -1, 2, -3, 1 };
int n = sizeof(arr) / sizeof(arr[0]);
findTriplets(arr, n);
return 0;
}