aboutsummaryrefslogtreecommitdiff
path: root/challenge-199/deadmarshal/cpp
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2023-01-10 18:42:08 +0000
committerGitHub <noreply@github.com>2023-01-10 18:42:08 +0000
commitb78239e0130d4c3a0d1cc37b31fc78b2147c02da (patch)
tree27af15da04ed1e36c33938dd49ca27569451163e /challenge-199/deadmarshal/cpp
parent9848137785315a225734118aae7d0226a325b320 (diff)
parent0e3e1a42869013d09eaa4c797cd957539004d944 (diff)
downloadperlweeklychallenge-club-b78239e0130d4c3a0d1cc37b31fc78b2147c02da.tar.gz
perlweeklychallenge-club-b78239e0130d4c3a0d1cc37b31fc78b2147c02da.tar.bz2
perlweeklychallenge-club-b78239e0130d4c3a0d1cc37b31fc78b2147c02da.zip
Merge pull request #7397 from deadmarshal/TWC199
TWC199
Diffstat (limited to 'challenge-199/deadmarshal/cpp')
-rw-r--r--challenge-199/deadmarshal/cpp/ch-1.cpp22
-rw-r--r--challenge-199/deadmarshal/cpp/ch-2.cpp28
2 files changed, 50 insertions, 0 deletions
diff --git a/challenge-199/deadmarshal/cpp/ch-1.cpp b/challenge-199/deadmarshal/cpp/ch-1.cpp
new file mode 100644
index 0000000000..b5ccda1df0
--- /dev/null
+++ b/challenge-199/deadmarshal/cpp/ch-1.cpp
@@ -0,0 +1,22 @@
+#include<iostream>
+#include<vector>
+
+template<typename T>
+int good_pairs(const std::vector<T> &vec)
+{
+ int count{};
+ for(size_t i = 0; i < vec.size(); ++i)
+ for(size_t j = i+1; j < vec.size(); ++j)
+ if(vec[i] == vec[j]) count++;
+ return count;
+}
+
+int main()
+{
+ std::vector<int> vec1{1,2,3,1,1,3},vec2{1,2,3},vec3{1,1,1,1};
+ std::cout << good_pairs<int>(vec1) << '\n';
+ std::cout << good_pairs<int>(vec2) << '\n';
+ std::cout << good_pairs<int>(vec3) << '\n';
+ return 0;
+}
+
diff --git a/challenge-199/deadmarshal/cpp/ch-2.cpp b/challenge-199/deadmarshal/cpp/ch-2.cpp
new file mode 100644
index 0000000000..cfb5147495
--- /dev/null
+++ b/challenge-199/deadmarshal/cpp/ch-2.cpp
@@ -0,0 +1,28 @@
+#include<iostream>
+#include<vector>
+
+template<typename T>
+int good_triplets(const std::vector<T> &vec, int x, int y, int z)
+{
+ int count{};
+ for(size_t i = 0; i < vec.size(); ++i)
+ for(size_t j = 0; j < vec.size(); ++j)
+ for(size_t k = 0; k < vec.size(); ++k)
+ if((abs(vec[i] - vec[j]) <= x) &&
+ (abs(vec[j] - vec[k]) <= y) &&
+ (abs(vec[i] - vec[k]) <= z) &&
+ (0 <= i) &&
+ (i < j) &&
+ (j < k) &&
+ (k <= vec.size())) count++;
+ return count;
+}
+
+int main()
+{
+ std::vector<int> vec1{3,0,1,1,9,7}, vec2{1,1,2,2,3};
+ std::cout << good_triplets<int>(vec1,7,2,3) << '\n';
+ std::cout << good_triplets<int>(vec2,0,0,1) << '\n';
+ return 0;
+}
+