-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3Sum.cpp
75 lines (73 loc) · 1.58 KB
/
3Sum.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
//Brute force
//TC O(N^3) SC O(1)
#include <set>
vector<vector<int>> findTriplets(vector<int> arr, int n, int K)
{
// Write your code here.
set<vector<int>> visited;
vector<vector<int>> res;
for (int i = 0; i < n - 2; i++)
{
for (int j = i + 1; j < n - 1; j++)
{
for (int m = j + 1; m < n; m++)
{
if (arr[i] + arr[j] + arr[m] == K)
{
vector<int> triplets;
triplets.push_back(arr[i]);
triplets.push_back(arr[j]);
triplets.push_back(arr[m]);
sort(triplets.begin(), triplets.end());
if (visited.find(triplets) == visited.end())
{
res.push_back(triplets);
visited.insert(triplets);
}
}
}
}
}
return res;
}
// Optimized Two Pointers
// TC O(N^2) SC O(1)
vector<vector<int>> findTriplets(vector<int> arr, int n, int K)
{
vector<vector<int>> ans;
sort(arr.begin(), arr.end());
for (int i = 0; i < n; i++)
{
int target = K - arr[i];
int front = i + 1;
int back = n - 1;
while (front < back)
{
int sum = arr[front] + arr[back];
if (sum < target)
front++;
else if (sum > target)
back--;
else
{
int x = arr[front];
int y = arr[back];
ans.push_back({arr[i], arr[front], arr[back]});
// increamenting front ponter until we reach a diff number
while (front < back && arr[front] == x)
{
front++;
}
// decreamenting back pointer until we reach a diff number
while (front < back && arr[back] == y)
{
back--;
}
}
}
// Ensuring that we don't encounter duplicate values fro arr[i]
while (i + 1 < n && arr[i] == arr[i + 1])
i++;
}
return ans;
}