-
Notifications
You must be signed in to change notification settings - Fork 3
/
All unique triplets that sum up to a given value
123 lines (103 loc) · 2.59 KB
/
All unique triplets that sum up to a given value
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// C++ program to find unique triplets
// that sum up to a given value.
#include <bits/stdc++.h>
using namespace std;
// Structure to define a triplet.
struct triplet
{
int first, second, third;
};
// Function to find unique triplets that
// sum up to a given value.
int findTriplets(int nums[], int n, int sum)
{
int i, j, k;
// Vector to store all unique triplets.
vector<triplet> triplets;
// Set to store already found triplets
// to avoid duplication.
unordered_set<string> uniqTriplets;
// Variable used to hold triplet
// converted to string form.
string temp;
// Variable used to store current
// triplet which is stored in vector
// if it is unique.
triplet newTriplet;
// Sort the input array.
sort(nums, nums + n);
// Iterate over the array from the
// start and consider it as the
// first element.
for (i = 0; i < n - 2; i++)
{
// index of the first element in
// the remaining elements.
j = i + 1;
// index of the last element.
k = n - 1;
while (j < k) {
// If sum of triplet is equal to
// given value, then check if
// this triplet is unique or not.
// To check uniqueness, convert
// triplet to string form and
// then check if this string is
// present in set or not. If
// triplet is unique, then store
// it in vector.
if (nums[i] + nums[j] + nums[k] == sum)
{
temp = to_string(nums[i]) + " : "
+ to_string(nums[j]) + " : "
+ to_string(nums[k]);
if (uniqTriplets.find(temp)
== uniqTriplets.end()) {
uniqTriplets.insert(temp);
newTriplet.first = nums[i];
newTriplet.second = nums[j];
newTriplet.third = nums[k];
triplets.push_back(newTriplet);
}
// Increment the first index
// and decrement the last
// index of remaining elements.
j++;
k--;
}
// If sum is greater than given
// value then to reduce sum
// decrement the last index.
else if (nums[i] + nums[j] + nums[k] > sum)
k--;
// If sum is less than given value
// then to increase sum increment
// the first index of remaining
// elements.
else
j++;
}
}
// If no unique triplet is found, then
// return 0.
if (triplets.size() == 0)
return 0;
// Print all unique triplets stored in
// vector.
for (i = 0; i < triplets.size(); i++) {
cout << "[" << triplets[i].first << ", "
<< triplets[i].second << ", "
<< triplets[i].third << "], ";
}
}
// Driver Code
int main()
{
int nums[] = { 12, 3, 6, 1, 6, 9 };
int n = sizeof(nums) / sizeof(nums[0]);
int sum = 24;
// Function call
if (!findTriplets(nums, n, sum))
cout << "No triplets can be formed.";
return 0;
}