From 2405a1423b1620a18dddcecfadc64d400edb31e9 Mon Sep 17 00:00:00 2001 From: Swastika Gupta <64654203+Swastyy@users.noreply.github.com> Date: Sat, 10 Jul 2021 12:51:50 +0530 Subject: [PATCH] Update subset_sum.cpp --- backtracking/subset_sum.cpp | 27 +++++++++++---------------- 1 file changed, 11 insertions(+), 16 deletions(-) diff --git a/backtracking/subset_sum.cpp b/backtracking/subset_sum.cpp index 8f9c149da..0522bfbdd 100644 --- a/backtracking/subset_sum.cpp +++ b/backtracking/subset_sum.cpp @@ -20,8 +20,7 @@ namespace backtracking { /** * @namespace Subsets - * @brief Functions for the [Subset - * Sum](https://en.wikipedia.org/wiki/Subset_sum_problem) problem. + * @brief Functions for the [Subset Sum](https://en.wikipedia.org/wiki/Subset_sum_problem) problem. */ namespace subset_sum { /** @@ -31,7 +30,7 @@ namespace subset_sum { * @returns count of the number of subsets with required sum */ -uint64_t subset_sum(int64_t sum, const std::vector &in_arr) { +uint64_t subset_sum(int sum, const std::vector &in_arr) { int nelement = in_arr.size(); int count_of_subset = 0; @@ -58,35 +57,31 @@ uint64_t subset_sum(int64_t sum, const std::vector &in_arr) { static void test() { // Test 1 std::cout << "1st test "; - std::vector array1 = {-7, -3, -2, 5, 8}; // input array - assert(backtracking::subset_sum::subset_sum(0, array1) == 2); // first argument in subset_sum function is the required sum and - // second is the input array + std::vector array1 = {-7, -3, -2, 5, 8}; // input array + assert(backtracking::subset_sum::subset_sum(0, array1) == 2); // first argument in subset_sum function is the required sum and second is the input array std::cout << "passed" << std::endl; // Test 2 std::cout << "2nd test "; - std::vector array2 = {1, 2, 3, 3}; - assert(backtracking::subset_sum::subset_sum(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::vector array2 = {1, 2, 3, 3}; + assert(backtracking::subset_sum::subset_sum(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; // Test 3 std::cout << "3rd test "; - std::vector array3 = {1, 1, 1, 1}; - assert(backtracking::subset_sum::subset_sum(1, array3) == 4); // here we are expecting 4 subsets which sum up to 1 i.e. - // {(1),(1),(1),(1)} + std::vector array3 = {1, 1, 1, 1}; + assert(backtracking::subset_sum::subset_sum(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; // Test 4 std::cout << "4th test "; - std::vector array4 = {3, 3, 3, 3}; - assert(backtracking::subset_sum::subset_sum(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::vector array4 = {3, 3, 3, 3}; + assert(backtracking::subset_sum::subset_sum(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 = {}; + std::vector array5 = {}; assert(backtracking::subset_sum::subset_sum(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; }