From aadecfe4484319aaee42c5c05423f0d23662d5d6 Mon Sep 17 00:00:00 2001 From: deadmarshal Date: Sat, 30 Mar 2024 10:44:43 +0330 Subject: TWC262 --- challenge-262/deadmarshal/cpp/ch-1.cpp | 21 +++++++++++++++++++++ challenge-262/deadmarshal/cpp/ch-2.cpp | 21 +++++++++++++++++++++ 2 files changed, 42 insertions(+) create mode 100644 challenge-262/deadmarshal/cpp/ch-1.cpp create mode 100644 challenge-262/deadmarshal/cpp/ch-2.cpp (limited to 'challenge-262/deadmarshal/cpp') diff --git a/challenge-262/deadmarshal/cpp/ch-1.cpp b/challenge-262/deadmarshal/cpp/ch-1.cpp new file mode 100644 index 0000000000..391ea1f286 --- /dev/null +++ b/challenge-262/deadmarshal/cpp/ch-1.cpp @@ -0,0 +1,21 @@ +#include +#include + +template +std::size_t max_positive_negative(const std::vector &vec) +{ + std::size_t neg{},pos{}; + for(const T& e : vec) if(e < 0) neg++; else pos++; + return static_cast(std::max(neg,pos)); +} + +int main() +{ + std::vector vec1{-3,1,2,-1,3,-2,4}, + vec2{-1,-2,-3,1},vec3{1,2}; + std::cout << max_positive_negative(vec1) << '\n' + << max_positive_negative(vec2) << '\n' + << max_positive_negative(vec3) << '\n'; + return 0; +} + diff --git a/challenge-262/deadmarshal/cpp/ch-2.cpp b/challenge-262/deadmarshal/cpp/ch-2.cpp new file mode 100644 index 0000000000..b5da7bc33c --- /dev/null +++ b/challenge-262/deadmarshal/cpp/ch-2.cpp @@ -0,0 +1,21 @@ +#include +#include + +template +std::size_t count_equal_divisible(const std::vector &vec,int k) +{ + size_t count{}; + for(size_t i = 0; i < vec.size()-1; ++i) + for(size_t j = i+1; j < vec.size(); ++j) + if((vec.at(i) == vec.at(j)) && ((i*j) % k == 0)) count++; + return count; +} + +int main() +{ + std::vector vec1{3,1,2,2,2,1,3},vec2{1,2,3}; + std::cout << count_equal_divisible(vec1,2) << '\n' + << count_equal_divisible(vec2,1) << '\n'; + return 0; +} + -- cgit