aboutsummaryrefslogtreecommitdiff
path: root/challenge-195/deadmarshal/cpp
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-195/deadmarshal/cpp')
-rw-r--r--challenge-195/deadmarshal/cpp/ch-1.cpp35
-rw-r--r--challenge-195/deadmarshal/cpp/ch-2.cpp36
2 files changed, 71 insertions, 0 deletions
diff --git a/challenge-195/deadmarshal/cpp/ch-1.cpp b/challenge-195/deadmarshal/cpp/ch-1.cpp
new file mode 100644
index 0000000000..c2efa674ec
--- /dev/null
+++ b/challenge-195/deadmarshal/cpp/ch-1.cpp
@@ -0,0 +1,35 @@
+#include<iostream>
+#include<vector>
+#include<algorithm>
+
+std::vector<int> dtov(int n)
+{
+ std::vector<int> digits{};
+ while(n)
+ {
+ digits.push_back(n % 10);
+ n /= 10;
+ }
+ return digits;
+}
+
+int special_integers(int n)
+{
+ int count{};
+ for(int i = 1; i <= n; ++i)
+ {
+ std::vector<int> digits = dtov(i);
+ const bool has_dups =
+ std::adjacent_find(digits.begin(), digits.end()) != digits.end();
+ if(!has_dups) count++;
+ }
+ return count;
+}
+
+int main()
+{
+ std::cout << special_integers(15) << '\n';
+ std::cout << special_integers(35) << '\n';
+ return 0;
+}
+
diff --git a/challenge-195/deadmarshal/cpp/ch-2.cpp b/challenge-195/deadmarshal/cpp/ch-2.cpp
new file mode 100644
index 0000000000..610a1bdf62
--- /dev/null
+++ b/challenge-195/deadmarshal/cpp/ch-2.cpp
@@ -0,0 +1,36 @@
+#include<iostream>
+#include<vector>
+#include<map>
+#include<algorithm>
+
+int most_frequent_even(std::vector<int> vec)
+{
+ if(std::all_of(vec.begin(),
+ vec.end(),
+ [](int i){return i % 2 != 0;})) return -1;
+ vec.erase(std::remove_if(vec.begin(),
+ vec.end(),
+ [](int i){return i % 2 != 0;}),
+ vec.end());
+ std::map<int,int,std::greater<int>> map{};
+ std::vector<int> keys{},vals{};
+ for(const auto& i : vec) map[i]++;
+ for(const auto& kv : map)
+ {
+ keys.push_back(kv.first);
+ vals.push_back(kv.second);
+ }
+ const bool has_dups =
+ std::adjacent_find(vals.begin(), vals.end()) != vals.end();
+ if(!has_dups) return keys.back();
+ return keys.back();
+}
+
+int main()
+{
+ std::cout << most_frequent_even({1,1,2,6,2}) << '\n';
+ std::cout << most_frequent_even({1,3,5,7}) << '\n';
+ std::cout << most_frequent_even({6,4,4,6,1}) << '\n';
+ return 0;
+}
+