From eb979e8967291a018e4723b2f18a82f066b3d0e5 Mon Sep 17 00:00:00 2001 From: DaveRoox Date: Sat, 13 Oct 2018 11:51:36 +0200 Subject: [PATCH 1/3] Solution to a-power-b --- .../smart-interviews/a-power-b/input.txt | 5 ++++ .../smart-interviews/a-power-b/main.cpp | 30 +++++++++++++++++++ 2 files changed, 35 insertions(+) create mode 100644 hackerrank/smart-interviews/a-power-b/input.txt create mode 100644 hackerrank/smart-interviews/a-power-b/main.cpp diff --git a/hackerrank/smart-interviews/a-power-b/input.txt b/hackerrank/smart-interviews/a-power-b/input.txt new file mode 100644 index 0000000..924bb7d --- /dev/null +++ b/hackerrank/smart-interviews/a-power-b/input.txt @@ -0,0 +1,5 @@ +4 +5 2 +1 10 +2 30 +10 10 \ No newline at end of file diff --git a/hackerrank/smart-interviews/a-power-b/main.cpp b/hackerrank/smart-interviews/a-power-b/main.cpp new file mode 100644 index 0000000..1f2a233 --- /dev/null +++ b/hackerrank/smart-interviews/a-power-b/main.cpp @@ -0,0 +1,30 @@ +#include + +using namespace std; + +constexpr const unsigned long mod = 1000000007; + +unsigned long power(unsigned short a, unsigned short b) { + if(b == 0) + return 1ul; + if(b == 1) + return static_cast(a); + unsigned long hpower = power(a, b >> 1); + return (hpower * ((hpower * (b % 2 == 1 ? a : 1)) % mod)) % mod; +} + +int main() { + + unsigned int T; + unsigned short a, b; + fstream fin("../input.txt"), fout("../output.txt", ios::out); + + fin >> T; + + while(T--) + fin >> a >> b, fout << power(a, b) << endl; + + fin.close(), fout.close(); + + return 0; +} \ No newline at end of file From a12b5eba3b2fcc245dc2afb9f606dd0b0476068d Mon Sep 17 00:00:00 2001 From: DaveRoox Date: Sat, 13 Oct 2018 14:23:05 +0200 Subject: [PATCH 2/3] Solution to a-power-b --- .../input.txt | 7 ++ .../main.cpp | 114 ++++++++++++++++++ 2 files changed, 121 insertions(+) create mode 100644 hackerrank/smart-interviews/sum-of-numbers-from-root-to-leaf-paths/input.txt create mode 100644 hackerrank/smart-interviews/sum-of-numbers-from-root-to-leaf-paths/main.cpp diff --git a/hackerrank/smart-interviews/sum-of-numbers-from-root-to-leaf-paths/input.txt b/hackerrank/smart-interviews/sum-of-numbers-from-root-to-leaf-paths/input.txt new file mode 100644 index 0000000..6c8bb93 --- /dev/null +++ b/hackerrank/smart-interviews/sum-of-numbers-from-root-to-leaf-paths/input.txt @@ -0,0 +1,7 @@ +3 +5 +1 2 3 4 5 +5 +3 2 4 1 5 +7 +4 5 15 2 1 7 17 \ No newline at end of file diff --git a/hackerrank/smart-interviews/sum-of-numbers-from-root-to-leaf-paths/main.cpp b/hackerrank/smart-interviews/sum-of-numbers-from-root-to-leaf-paths/main.cpp new file mode 100644 index 0000000..fc7a2b5 --- /dev/null +++ b/hackerrank/smart-interviews/sum-of-numbers-from-root-to-leaf-paths/main.cpp @@ -0,0 +1,114 @@ +#include +#include + +using namespace std; +using elem_type = unsigned int; + +constexpr const unsigned long mod = 1000000007; + +template +class BST { + + using elem_type = T; + using return_type = R; + +public: + + BST(): root(nullptr) {} + + explicit BST(const vector & v): root(nullptr) { + for(const elem_type & elem : v) + this->add(elem); + } + + inline ~BST() { + delete this->root; + } + + void add(elem_type elem) { + if(this->root) + this->root->add(elem); + else + this->root = new BSTNode(elem); + } + + inline return_type get_sums() const { + if(!this->root) + return { static_cast(0) }; + return this->root->get_sums(); + } + +private: + + struct BSTNode { + + elem_type elem; + BSTNode *right, *left; + + explicit BSTNode(elem_type t_elem): + elem(move(t_elem)), + right(nullptr), + left(nullptr) + {} + + inline ~BSTNode() { + delete this->left, delete this->right; + } + + void add(elem_type elem) { + if(elem < this->elem) { + if(!this->left) + this->left = new BSTNode(elem); + else + this->left->add(elem); + } + else { + if(!this->right) + this->right = new BSTNode(elem); + else + this->right->add(elem); + } + } + + return_type get_sums(return_type curr_value = static_cast(0)) const { + + elem_type elem = this->elem; + + while(elem) + curr_value = (curr_value * 10) % mod, elem /= 10; + + return_type new_value = (curr_value + this->elem) % mod; + + if(!this->left and !this->right) + return new_value; + + return_type left_result = this->left ? this->left->get_sums(new_value) : static_cast(0); + return_type right_result = this->right ? this->right->get_sums(new_value) : static_cast(0); + return (left_result + right_result) % mod; + } + + }; + + BSTNode * root; +}; + +int main() { + + unsigned int T, N; + fstream fin("../input.txt"), fout("../output.txt", ios::out); + + fin >> T; + + while(T--) { + fin >> N; + elem_type tmp; + vector v; + while(N--) + fin >> tmp, v.push_back(tmp); + fout << BST{v}.get_sums() << endl; + } + + fin.close(), fout.close(); + + return 0; +} \ No newline at end of file From b78e8d62d68a02e0c2cfed309eb7cd1da88de583 Mon Sep 17 00:00:00 2001 From: DaveRoox Date: Sat, 13 Oct 2018 14:26:26 +0200 Subject: [PATCH 3/3] Solution to sum-of-numbers-from-root-to-leaf-paths --- .../sum-of-numbers-from-root-to-leaf-paths/main.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/hackerrank/smart-interviews/sum-of-numbers-from-root-to-leaf-paths/main.cpp b/hackerrank/smart-interviews/sum-of-numbers-from-root-to-leaf-paths/main.cpp index fc7a2b5..3c2470c 100644 --- a/hackerrank/smart-interviews/sum-of-numbers-from-root-to-leaf-paths/main.cpp +++ b/hackerrank/smart-interviews/sum-of-numbers-from-root-to-leaf-paths/main.cpp @@ -2,6 +2,7 @@ #include using namespace std; + using elem_type = unsigned int; constexpr const unsigned long mod = 1000000007;