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 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..3c2470c --- /dev/null +++ b/hackerrank/smart-interviews/sum-of-numbers-from-root-to-leaf-paths/main.cpp @@ -0,0 +1,115 @@ +#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