From 79df3f502658e9e7622ce49faa32ee703d936f67 Mon Sep 17 00:00:00 2001 From: deadmarshal Date: Sat, 28 Jan 2023 20:44:13 +0330 Subject: TWC201 --- challenge-201/deadmarshal/cpp/ch-1.cpp | 22 ++++++++++++++++++++++ challenge-201/deadmarshal/cpp/ch-2.cpp | 31 +++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+) create mode 100644 challenge-201/deadmarshal/cpp/ch-1.cpp create mode 100644 challenge-201/deadmarshal/cpp/ch-2.cpp (limited to 'challenge-201/deadmarshal/cpp') diff --git a/challenge-201/deadmarshal/cpp/ch-1.cpp b/challenge-201/deadmarshal/cpp/ch-1.cpp new file mode 100644 index 0000000000..c1b8a37d4f --- /dev/null +++ b/challenge-201/deadmarshal/cpp/ch-1.cpp @@ -0,0 +1,22 @@ +#include +#include +#include + +template +void missing_numbers(const std::vector &vec) +{ + std::unordered_map m{}; + for(size_t i = 0; i < vec.size(); ++i) m[vec[i]] = 1; + for(size_t i = 0; i < vec.size()+1; ++i) + if(m[i] != 1) std::cout << i << ' '; + std::cout << '\n'; +} + +int main() +{ + const std::vector vec1{0,1,3},vec2{0,1}; + missing_numbers(vec1); + missing_numbers(vec2); + return 0; +} + diff --git a/challenge-201/deadmarshal/cpp/ch-2.cpp b/challenge-201/deadmarshal/cpp/ch-2.cpp new file mode 100644 index 0000000000..d05bc157e5 --- /dev/null +++ b/challenge-201/deadmarshal/cpp/ch-2.cpp @@ -0,0 +1,31 @@ +#include + +#define N 5 +static int count{}; + +void find_combinations(int *arr, + int index, + int num, + int reduced_num) +{ + if(reduced_num < 0) return; + if(reduced_num == 0) + { + count++; + return; + } + int prev = index == 0 ? 1 : arr[index-1]; + for(int i = prev; i <= num; ++i) + { + arr[index] = i; + find_combinations(arr,index+1,num,reduced_num-i); + } +} + +int main() +{ + int arr[N]; + find_combinations(arr,0,N,N); + std::cout << count << '\n'; +} + -- cgit