From e059765f79d5a6dffe6b7554f53e030dfc2e7e2d Mon Sep 17 00:00:00 2001 From: Swastika Gupta <64654203+Swastyy@users.noreply.github.com> Date: Tue, 13 Jul 2021 06:49:03 +0530 Subject: [PATCH] Create subset_sum.cpp (#1517) * Create subset_sum.cpp * Update subset_sum.cpp * Update subset_sum.cpp * updating DIRECTORY.md * clang-format and clang-tidy fixes for 4827805a * Update subset_sum.cpp * clang-format and clang-tidy fixes for dac32465 * Update backtracking/subset_sum.cpp Co-authored-by: Filip Hlasek * Update backtracking/subset_sum.cpp Co-authored-by: Filip Hlasek * clang-format and clang-tidy fixes for 1328571c * Update backtracking/subset_sum.cpp Co-authored-by: Filip Hlasek * Update subset_sum.cpp * clang-format and clang-tidy fixes for 2325e165 * Update backtracking/subset_sum.cpp Co-authored-by: Abhinn Mishra <49574460+mishraabhinn@users.noreply.github.com> * Update backtracking/subset_sum.cpp Co-authored-by: Abhinn Mishra <49574460+mishraabhinn@users.noreply.github.com> * clang-format and clang-tidy fixes for 1b82d499 * Update backtracking/subset_sum.cpp Co-authored-by: David Leal * Update backtracking/subset_sum.cpp Co-authored-by: David Leal * Update backtracking/subset_sum.cpp Co-authored-by: David Leal * clang-format and clang-tidy fixes for 31a1deae * Update backtracking/subset_sum.cpp Co-authored-by: David Leal * Update backtracking/subset_sum.cpp Co-authored-by: David Leal * Update backtracking/subset_sum.cpp Co-authored-by: David Leal * Update backtracking/subset_sum.cpp Co-authored-by: David Leal * Update backtracking/subset_sum.cpp Co-authored-by: David Leal * Update backtracking/subset_sum.cpp Co-authored-by: David Leal * Update backtracking/subset_sum.cpp Co-authored-by: David Leal * Update backtracking/subset_sum.cpp Co-authored-by: David Leal * Update backtracking/subset_sum.cpp Co-authored-by: David Leal * Update backtracking/subset_sum.cpp Co-authored-by: David Leal * Update subset_sum.cpp * clang-format and clang-tidy fixes for 9cd07635 * Update backtracking/subset_sum.cpp Co-authored-by: Abhinn Mishra <49574460+mishraabhinn@users.noreply.github.com> * Update backtracking/subset_sum.cpp Co-authored-by: Abhinn Mishra <49574460+mishraabhinn@users.noreply.github.com> * Update backtracking/subset_sum.cpp Co-authored-by: Abhinn Mishra <49574460+mishraabhinn@users.noreply.github.com> * Update backtracking/subset_sum.cpp Co-authored-by: Abhinn Mishra <49574460+mishraabhinn@users.noreply.github.com> * Update backtracking/subset_sum.cpp Co-authored-by: Abhinn Mishra <49574460+mishraabhinn@users.noreply.github.com> * Update backtracking/subset_sum.cpp Co-authored-by: Abhinn Mishra <49574460+mishraabhinn@users.noreply.github.com> * Update subset_sum.cpp * Update subset_sum.cpp * clang-format and clang-tidy fixes for 2405a142 * Update subset_sum.cpp * clang-format and clang-tidy fixes for 383baeb3 * Update subset_sum.cpp * Update backtracking/subset_sum.cpp * Update backtracking/subset_sum.cpp * Update backtracking/subset_sum.cpp * Update backtracking/subset_sum.cpp * Update backtracking/subset_sum.cpp * Update backtracking/subset_sum.cpp * Update backtracking/subset_sum.cpp Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Co-authored-by: Filip Hlasek Co-authored-by: Abhinn Mishra <49574460+mishraabhinn@users.noreply.github.com> Co-authored-by: David Leal --- DIRECTORY.md | 1 + backtracking/subset_sum.cpp | 106 ++++++++++++++++++++++++++++++++++++ 2 files changed, 107 insertions(+) create mode 100644 backtracking/subset_sum.cpp diff --git a/DIRECTORY.md b/DIRECTORY.md index ea7ed6779..f56b46e2e 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -7,6 +7,7 @@ * [N Queens All Solution Optimised](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/backtracking/n_queens_all_solution_optimised.cpp) * [Nqueen Print All Solutions](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/backtracking/nqueen_print_all_solutions.cpp) * [Rat Maze](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/backtracking/rat_maze.cpp) + * [Subset Sum](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/backtracking/subset_sum.cpp) * [Sudoku Solve](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/backtracking/sudoku_solve.cpp) ## Bit Manipulation diff --git a/backtracking/subset_sum.cpp b/backtracking/subset_sum.cpp new file mode 100644 index 000000000..fad820920 --- /dev/null +++ b/backtracking/subset_sum.cpp @@ -0,0 +1,106 @@ +/** + * @file + * @brief Implementation of the [Subset + * Sum](https://en.wikipedia.org/wiki/Subset_sum_problem) problem. + * @details + * We are given an array and a sum value. The algorithm finds all + * the subsets of that array with sum equal to the given sum and return such + * subsets count. This approach will have exponential time complexity. + * @author [Swastika Gupta](https://github.com/Swastyy) + */ + +#include /// for assert +#include /// for IO operations +#include /// for std::vector + +/** + * @namespace backtracking + * @brief Backtracking algorithms + */ +namespace backtracking { +/** + * @namespace Subsets + * @brief Functions for the [Subset + * Sum](https://en.wikipedia.org/wiki/Subset_sum_problem) problem. + */ +namespace subset_sum { +/** + * @brief The main function implements count of subsets + * @param sum is the required sum of any subset + * @param in_arr is the input array + * @returns count of the number of subsets with required sum + */ +uint64_t number_of_subsets(int32_t sum, const std::vector &in_arr) { + int32_t nelement = in_arr.size(); + uint64_t count_of_subset = 0; + + for (int32_t i = 0; i < (1 << (nelement)); i++) { + int32_t check = 0; + for (int32_t j = 0; j < nelement; j++) { + if (i & (1 << j)) { + check += (in_arr[j]); + } + } + if (check == sum) { + count_of_subset++; + } + } + return count_of_subset; +} +} // namespace subset_sum +} // namespace backtracking + +/** + * @brief Test implementations + * @returns void + */ +static void test() { + // 1st test + std::cout << "1st test "; + std::vector array1 = {-7, -3, -2, 5, 8}; // input array + assert(backtracking::subset_sum::number_of_subsets(0, array1) == + 2); // first argument in subset_sum function is the required sum and + // second is the input array + std::cout << "passed" << std::endl; + + // 2nd test + std::cout << "2nd test "; + std::vector array2 = {1, 2, 3, 3}; + assert(backtracking::subset_sum::number_of_subsets(6, array2) == + 3); // here we are expecting 3 subsets which sum up to 6 i.e. + // {(1,2,3),(1,2,3),(3,3)} + std::cout << "passed" << std::endl; + + // 3rd test + std::cout << "3rd test "; + std::vector array3 = {1, 1, 1, 1}; + assert(backtracking::subset_sum::number_of_subsets(1, array3) == + 4); // here we are expecting 4 subsets which sum up to 1 i.e. + // {(1),(1),(1),(1)} + std::cout << "passed" << std::endl; + + // 4th test + std::cout << "4th test "; + std::vector array4 = {3, 3, 3, 3}; + assert(backtracking::subset_sum::number_of_subsets(6, array4) == + 6); // here we are expecting 6 subsets which sum up to 6 i.e. + // {(3,3),(3,3),(3,3),(3,3),(3,3),(3,3)} + std::cout << "passed" << std::endl; + + // Test 5 + std::cout << "5th test "; + std::vector array5 = {}; + assert(backtracking::subset_sum::number_of_subsets(6, array5) == + 0); // here we are expecting 0 subsets which sum up to 6 i.e. we + // cannot select anything from an empty array + std::cout << "passed" << std::endl; +} + +/** + * @brief Main function + * @returns 0 on exit + */ +int main() { + test(); // run self-test implementations + return 0; +}