diff --git a/c++/Chapter 11/Chapter11_6_MD.cpp b/c++/Chapter 11/Chapter11_6_MD.cpp new file mode 100644 index 00000000..92c924be --- /dev/null +++ b/c++/Chapter 11/Chapter11_6_MD.cpp @@ -0,0 +1,52 @@ +// Program: Finding a number in a matrix ordered in ascendent order by row and by column; +// + +#include "stdafx.h" +#include +#include +#include +#include +#include + +using namespace std; +#define MAX_ELEMENTS 4 + +int matrix[MAX_ELEMENTS][MAX_ELEMENTS] = { + { 1, 5, 8, 11 }, + { 2, 7, 10, 13 }, + { 3, 9, 12, 15 }, + { 4, 11, 14, 17 } }; + +bool findNumInMatrix(int matrix[MAX_ELEMENTS][MAX_ELEMENTS], int size, int x) { + int row = 0; + int column = size - 1; + while (row < size && column >= 0) { + int value = matrix[row][column]; + if (value == x) { + return true; // found + } + //cout << "\t" << x; + if (value < x) { + ++row; // try a row with greater values + //cout << " > " << value << " going to next row" << endl; + } + else if (value > x) { + --column; // try a column with minor values + //cout << " < " << value << " going to previous column" << endl; + } + } + return false; +} + +void evaluate(int values[], int length) { + for (int ind = 0; ind < length; ++ind) + cout << "Value \'" << values[ind] << "\': " << + (findNumInMatrix(matrix, MAX_ELEMENTS, values[ind]) ? "Found" : "Not found") << endl; +} + +int main() { + int values[6] = { 12, 17, 20, 0, 11, 4 }; + evaluate(values, 6); + return 0; +} + diff --git a/c++/Chapter 5/Question5_1-MD.cpp b/c++/Chapter 5/Question5_1-MD.cpp new file mode 100644 index 00000000..6d2e30f1 --- /dev/null +++ b/c++/Chapter 5/Question5_1-MD.cpp @@ -0,0 +1,40 @@ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace std; + +string toBinary(int n){ + int bits = static_cast(floor(log2(n)))+1; + string str(bits, '0'); + for (int i = bits-1, j = 0; j < bits; --i, ++j){ + str[i] = (n & (1 << j)) == 0 ? '0' : '1' ; + } + return str; +} + +int main(){ + std::ios::sync_with_stdio(false); // http://en.cppreference.com/w/cpp/io/ios_base/sync_with_stdio + cin.tie(NULL); + + int N, M, i, j; // good examples: N = 1024 or 1041, M = 19, i = 2, j = 6. + cin >> N >> M >> i >> j; + + cout << "N = " << toBinary(N) <<".\tM = " << toBinary(M) << endl; + + M <<= i; + int mask = (~0 << j+1) | (1 << i)-1; // clear space for M + N &= mask; + N |= M; + + cout << N << " = " << toBinary(N) << endl; + + return 0; +} \ No newline at end of file diff --git a/c++/Chapter 5/main b/c++/Chapter 5/main new file mode 100644 index 00000000..2417ac7d Binary files /dev/null and b/c++/Chapter 5/main differ diff --git a/c++/mysolutions/10.1MergeSortedArrays.cpp b/c++/mysolutions/10.1MergeSortedArrays.cpp new file mode 100644 index 00000000..40533627 --- /dev/null +++ b/c++/mysolutions/10.1MergeSortedArrays.cpp @@ -0,0 +1,50 @@ +// Conversion: write a function to determine the number of bits you would need +// to flip the convert integer A to interget B. +// Example: 29 (11101) to 15 (01111) = 2 + +//#include "stdafx.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace std; + +void mergeSortedArrays(int arrayA[], size_t lenA, int arrayB[], size_t lenB) { + size_t totalLength = lenA + lenB; + --lenA; + --lenB; + for (size_t i = totalLength - 1; i >= 0 && i < totalLength; --i) { + if (arrayA[lenA] > arrayB[lenB]) { + arrayA[i] = arrayA[lenA--]; + } else { + arrayA[i] = arrayB[lenB--]; + } + } +} + +void printArray(int arr[], size_t len) { + for (int i = 0; i < len; ++i) { + cout << arr[i] << ", "; + } + cout << endl; +} + +int main() { + int a[15] = { 1,2,3,7,8,9,12 }; + int b[] = { 3,4,5, 6,10,11 }; + int lenA = 7, lenB = sizeof(b) / sizeof(int); + printArray(a, lenA); + printArray(b, lenB); + + mergeSortedArrays(a, lenA, b, lenB); + printArray(a, lenA+lenB); + + return EXIT_SUCCESS; +} + diff --git a/c++/mysolutions/4.8FirstCommonAncestor.cpp b/c++/mysolutions/4.8FirstCommonAncestor.cpp new file mode 100644 index 00000000..69e8c1fc --- /dev/null +++ b/c++/mysolutions/4.8FirstCommonAncestor.cpp @@ -0,0 +1,137 @@ +//#include "stdafx.h" +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace std; + +template +struct Node { + T data; + Node *left = nullptr, *right = nullptr; + + Node(const T &val) : data(val) { } + ~Node() { + if (left) { + delete left; + left = nullptr; + } + if (right) { + delete right; + right = nullptr; + } + } + Node *addLeftNode(const T &val) { + left = new Node(val); + return left; + } + Node *addRightNode(const T &val) { + right = new Node(val); + return right; + } +}; + +Node *loadTree() { + Node *root = new Node('a'); + auto b = root->addLeftNode('b'); + auto c = root->addRightNode('c'); + + auto d = b->addLeftNode('d'); + auto e = b->addRightNode('e'); + + e->addLeftNode('f'); + e->addRightNode('g'); + + auto h = c->addLeftNode('h'); + auto i = c->addRightNode('i'); + + h->addLeftNode('j'); + + return root; +} + +template +void printInOrder(Node *node) { + if (node == nullptr) + return; + + printInOrder(node->left); + cout << node->data << ", "; + printInOrder(node->right); +} + +template +void printPreOrder(const Node *node) { + if (node == nullptr) + return; + + cout << node->data << ", "; + printPreOrder(node->left); + printPreOrder(node->right); +} + +template +bool lookFor(const Node *node, const T &val, vector*> &path) { + if (node == nullptr) { + return false; + } + path.push_back(node); + if (node->data == val) { + return true; + } + + if (lookFor(node->left, val, path)) + return true; + if (lookFor(node->right, val, path)) + return true; + + path.pop_back(); + return false; +} + +template +vector*> findPath(const Node *node, const T &val) { + vector*> path; + lookFor(node, val, path); + return path; +} + + +template +const Node* getFirstCommonAncestor(const Node *root, const T &val1, const T&val2) { + vector*> path1 = findPath(root, val1); + vector*> path2 = findPath(root, val2); + int minPathLength = path1.size() < path2.size() ? path1.size() : path2.size(); + + for (int i = 0; i < minPathLength; ++i) { + if (path1[i]->data != path2[i]->data) { + return i > 0 ? path1[i - 1] : nullptr; + } + } + return nullptr; +} + + +int main() { + shared_ptr> root(loadTree()); + printPreOrder(root.get()); + cout << endl << endl; + + char val1 = 'd', val2 = 'g'; + const Node *antecestor = getFirstCommonAncestor(root.get(), val1, val2); + char defaultVal = char(); + if (antecestor != nullptr) { + cout << "First Common Ancestor of node: \'" << val1 << "\' and \'" << val2 << "\' is: \t" << antecestor->data << endl; + } + else { + cout << "No common ancestor found" << endl; + } + + return EXIT_SUCCESS; +} + diff --git a/c++/mysolutions/5.6BitFlips.cpp b/c++/mysolutions/5.6BitFlips.cpp new file mode 100644 index 00000000..abbcb6a3 --- /dev/null +++ b/c++/mysolutions/5.6BitFlips.cpp @@ -0,0 +1,35 @@ +// Conversion: write a function to determine the number of bits you would need +// to flip the convert integer A to interget B. +// Example: 29 (11101) to 15 (01111) = 2 + +// #include "stdafx.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace std; + +int numberOfFlips(int A, int B) { + int bitIndex = 0; + int flips = 0; + int numBits = std::max(ceil(log2(A)), ceil(log2(B))); + for (int i = 0; i < numBits; ++i) { + int a = A & (1 << i); + int b = B & (1 << i); + if (a != b) + ++flips; + } + return flips; +} + +int main() { + cout << numberOfFlips(29, 15) << endl; + return EXIT_SUCCESS; +} + diff --git a/java/Chapter 1/Question1_2/Question.java b/java/Chapter 1/Question1_2/Question.java new file mode 100644 index 00000000..a2eece74 --- /dev/null +++ b/java/Chapter 1/Question1_2/Question.java @@ -0,0 +1,26 @@ +package Question1_2; + +class Question { + + public static String reverse(String str) { + int length = str.length(); + StringBuilder output = new StringBuilder(length); + for(int i = length-1; i >= 0; --i) + output.append(str.charAt(i)); + return output.toString(); + } + + public static String reverse2(String str) { + StringBuilder output = new StringBuilder(str); + return output.reverse().toString(); + } + + public static void main(String[] args) { + String[] words = {"abcde", "cat"}; + for (String word : words) { + System.out.println("Reversing the string: "+word); + System.out.println("reverse of input string is: " + reverse2(word)); + } + } + +} \ No newline at end of file