From 5c9750d97b33fba6a6103a632315578e5feec3cf Mon Sep 17 00:00:00 2001 From: Alvin Philips Date: Sun, 24 Oct 2021 20:49:27 +0530 Subject: [PATCH] fix: Intersection of two arrays (#1781) * Create reverse_binary_tree.cpp * Added documentation Added Documentation for the level_order_traversal() function, and implemented a print() function to display the tree to STDOUT * Added documentation * Renamed tests to test * Fixed issue with incorrect using statement * updating DIRECTORY.md * clang-format and clang-tidy fixes for fb86292d * Added Test cases * Update operations_on_datastructures/reverse_binary_tree.cpp Co-authored-by: David Leal * Update operations_on_datastructures/reverse_binary_tree.cpp Co-authored-by: David Leal * Update operations_on_datastructures/reverse_binary_tree.cpp Co-authored-by: David Leal * Update operations_on_datastructures/reverse_binary_tree.cpp Co-authored-by: David Leal * Update operations_on_datastructures/reverse_binary_tree.cpp Co-authored-by: David Leal * Changed int to int64_t * Updated documentation wording * Reused code from union_of_two_arrays and modified for intersection * Fixed bug and corrected test output * Renamed file to better meet filename guidelines * updating DIRECTORY.md * clang-format and clang-tidy fixes for e653f77a * Changed integer type from int64_t to int32_t * Added reference to similar union_of_two_arrays file Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Co-authored-by: David Leal Co-authored-by: Abhinn Mishra <49574460+mishraabhinn@users.noreply.github.com> --- DIRECTORY.md | 2 +- .../intersection_of_2_arrays.cpp | 26 --- .../intersection_of_two_arrays.cpp | 203 ++++++++++++++++++ 3 files changed, 204 insertions(+), 27 deletions(-) delete mode 100644 operations_on_datastructures/intersection_of_2_arrays.cpp create mode 100644 operations_on_datastructures/intersection_of_two_arrays.cpp diff --git a/DIRECTORY.md b/DIRECTORY.md index 4480f7892..e8b2604d0 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -241,7 +241,7 @@ * [Circular Queue Using Array](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/operations_on_datastructures/circular_queue_using_array.cpp) * [Get Size Of Linked List](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/operations_on_datastructures/get_size_of_linked_list.cpp) * [Inorder Successor Of Bst](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/operations_on_datastructures/inorder_successor_of_bst.cpp) - * [Intersection Of 2 Arrays](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/operations_on_datastructures/intersection_of_2_arrays.cpp) + * [Intersection Of Two Arrays](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/operations_on_datastructures/intersection_of_two_arrays.cpp) * [Reverse A Linked List Using Recusion](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/operations_on_datastructures/reverse_a_linked_list_using_recusion.cpp) * [Reverse Binary Tree](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/operations_on_datastructures/reverse_binary_tree.cpp) * [Selectionsortlinkedlist](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/operations_on_datastructures/selectionsortlinkedlist.cpp) diff --git a/operations_on_datastructures/intersection_of_2_arrays.cpp b/operations_on_datastructures/intersection_of_2_arrays.cpp deleted file mode 100644 index 8a3b27edf..000000000 --- a/operations_on_datastructures/intersection_of_2_arrays.cpp +++ /dev/null @@ -1,26 +0,0 @@ -#include -int main() { - int i, j, m, n; - cout << "Enter size of array 1:"; - cin >> m; - cout << "Enter size of array 2:"; - cin >> n; - int a[m]; - int b[n]; - cout << "Enter elements of array 1:"; - for (i = 0; i < m; i++) cin >> a[i]; - for (i = 0; i < n; i++) cin >> b[i]; - i = 0; - j = 0; - while ((i < m) && (j < n)) { - if (a[i] < b[j]) - i++; - else if (a[i] > b[j]) - j++; - else { - cout << a[i++] << " "; - j++; - } - } - return 0; -} diff --git a/operations_on_datastructures/intersection_of_two_arrays.cpp b/operations_on_datastructures/intersection_of_two_arrays.cpp new file mode 100644 index 000000000..c660f505b --- /dev/null +++ b/operations_on_datastructures/intersection_of_two_arrays.cpp @@ -0,0 +1,203 @@ +/** + * @file + * @brief Implementation for the [Intersection of two sorted + * Arrays](https://en.wikipedia.org/wiki/Intersection_(set_theory)) + * algorithm. + * @details The intersection of two arrays is the collection of all the elements + * that are common in both the first and second arrays. This implementation uses + * ordered arrays, and an algorithm to correctly order them and return the + * result as a new array (vector). + * @see union_of_two_arrays.cpp + * @author [Alvin](https://github.com/polarvoid) + */ + +#include /// for std::sort +#include /// for assert +#include /// for IO operations +#include /// for std::vector + +/** + * @namespace operations_on_datastructures + * @brief Operations on Data Structures + */ +namespace operations_on_datastructures { + +/** + * @brief Prints the values of a vector sequentially, ending with a newline + * character. + * @param array Reference to the array to be printed + * @returns void + */ +void print(const std::vector &array) { + for (int32_t i : array) { + std::cout << i << " "; /// Print each value in the array + } + std::cout << "\n"; /// Print newline +} + +/** + * @brief Gets the intersection of two sorted arrays, and returns them in a + * vector. + * @details An algorithm is used that compares the elements of the two vectors, + * incrementing the index of the smaller of the two. If the elements are the + * same, the element is appended to the result array to be returned. + * @param first A std::vector of sorted integer values + * @param second A std::vector of sorted integer values + * @returns A std::vector of the intersection of the two arrays, in ascending + * order + */ +std::vector get_intersection(const std::vector &first, + const std::vector &second) { + std::vector res; ///< Vector to hold the intersection + size_t f_index = 0; ///< Index for the first array + size_t s_index = 0; ///< Index for the second array + size_t f_length = first.size(); ///< Length of first array + size_t s_length = second.size(); ///< Length of second array + + while (f_index < f_length && s_index < s_length) { + if (first[f_index] < second[s_index]) { + f_index++; ///< Increment index of second array + } else if (first[f_index] > second[s_index]) { + s_index++; ///< Increment index of second array + } else { + if ((res.size() == 0) || (first[f_index] != res.back())) { + res.push_back( + first[f_index]); ///< Add the element if it is unique + } + f_index++; ///< Increment index of first array + s_index++; ///< Increment index of second array too + } + } + return res; +} + +} // namespace operations_on_datastructures + +/** + * @namespace tests + * @brief Testcases to check intersection of Two Arrays. + */ +namespace tests { +using operations_on_datastructures::get_intersection; +using operations_on_datastructures::print; +/** + * @brief A Test to check an edge case (two empty arrays) + * @returns void + */ +void test1() { + std::cout << "TEST CASE 1\n"; + std::cout << "Intialized a = {} b = {}\n"; + std::cout << "Expected result: {}\n"; + std::vector a = {}; + std::vector b = {}; + std::vector result = get_intersection(a, b); + assert(result == a); ///< Check if result is empty + print(result); ///< Should only print newline + std::cout << "TEST PASSED!\n\n"; +} +/** + * @brief A Test to check an edge case (one empty array) + * @returns void + */ +void test2() { + std::cout << "TEST CASE 2\n"; + std::cout << "Intialized a = {} b = {2, 3}\n"; + std::cout << "Expected result: {}\n"; + std::vector a = {}; + std::vector b = {2, 3}; + std::vector result = get_intersection(a, b); + assert(result == a); ///< Check if result is equal to a + print(result); ///< Should only print newline + std::cout << "TEST PASSED!\n\n"; +} +/** + * @brief A Test to check correct functionality with a simple test case + * @returns void + */ +void test3() { + std::cout << "TEST CASE 3\n"; + std::cout << "Intialized a = {4, 6} b = {3, 6}\n"; + std::cout << "Expected result: {6}\n"; + std::vector a = {4, 6}; + std::vector b = {3, 6}; + std::vector result = get_intersection(a, b); + std::vector expected = {6}; + assert(result == expected); ///< Check if result is correct + print(result); ///< Should print 6 + std::cout << "TEST PASSED!\n\n"; +} +/** + * @brief A Test to check correct functionality with duplicate values + * @returns void + */ +void test4() { + std::cout << "TEST CASE 4\n"; + std::cout << "Intialized a = {4, 6, 6, 6} b = {2, 4, 4, 6}\n"; + std::cout << "Expected result: {4, 6}\n"; + std::vector a = {4, 6, 6, 6}; + std::vector b = {2, 4, 4, 6}; + std::vector result = get_intersection(a, b); + std::vector expected = {4, 6}; + assert(result == expected); ///< Check if result is correct + print(result); ///< Should print 4 6 + std::cout << "TEST PASSED!\n\n"; +} +/** + * @brief A Test to check correct functionality with a harder test case + * @returns void + */ +void test5() { + std::cout << "TEST CASE 5\n"; + std::cout << "Intialized a = {1, 2, 3, 4, 6, 7, 9} b = {2, 3, 4, 5}\n"; + std::cout << "Expected result: {2, 3, 4}\n"; + std::vector a = {1, 2, 3, 4, 6, 7, 9}; + std::vector b = {2, 3, 4, 5}; + std::vector result = get_intersection(a, b); + std::vector expected = {2, 3, 4}; + assert(result == expected); ///< Check if result is correct + print(result); ///< Should print 2 3 4 + std::cout << "TEST PASSED!\n\n"; +} +/** + * @brief A Test to check correct functionality with an array sorted using + * std::sort + * @returns void + */ +void test6() { + std::cout << "TEST CASE 6\n"; + std::cout << "Intialized a = {1, 3, 3, 2, 5, 9, 4, 7, 3, 2} "; + std::cout << "b = {11, 3, 7, 8, 6}\n"; + std::cout << "Expected result: {3, 7}\n"; + std::vector a = {1, 3, 3, 2, 5, 9, 4, 7, 3, 2}; + std::vector b = {11, 3, 7, 8, 6}; + std::sort(a.begin(), a.end()); ///< Sort vector a + std::sort(b.begin(), b.end()); ///< Sort vector b + std::vector result = get_intersection(a, b); + std::vector expected = {3, 7}; + assert(result == expected); ///< Check if result is correct + print(result); ///< Should print 3 7 + std::cout << "TEST PASSED!\n\n"; +} +} // namespace tests + +/** + * @brief Function to test the correctness of get_intersection() function + * @returns void + */ +static void test() { + tests::test1(); + tests::test2(); + tests::test3(); + tests::test4(); + tests::test5(); + tests::test6(); +} + +/** + * @brief main function + * @returns 0 on exit + */ +int main() { + test(); // run self-test implementations + return 0; +}