diff --git a/backtracking/subset_sum.cpp b/backtracking/subset_sum.cpp index 633bd8ff6..e9d4562be 100644 --- a/backtracking/subset_sum.cpp +++ b/backtracking/subset_sum.cpp @@ -18,6 +18,7 @@ namespace backtracking { /** * @namespace Subsets * @brief Functions for counting subsets in a given array with a given sum + * Time Complexity: O(sum*n), where the sum is the ‘target sum’ and ‘n’ is the size of the array. */ namespace Subsets { /** @@ -32,16 +33,12 @@ std::uint64_t subset_sum(int sum, const std::vector &in_arr) { int count_of_subset = 0; for (int i = 0; i < (1 << (nelement)); i++) { - std::vector subset; + int check = 0; for (int j = 0; j < nelement; j++) { if (i & (1 << j)) { - subset.push_back(in_arr[j]); + check += (in_arr[j]); } } - int check = 0; - for (int k : subset) { - check += k; - } if (check == sum) { count_of_subset++; } @@ -58,34 +55,32 @@ std::uint64_t subset_sum(int 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::Subsets::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::Subsets::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}; // input array - assert(backtracking::Subsets::subset_sum(6, array2) == - 3); // first argument in subset_sum function is the required sum and - // second is the input array + std::vector array2 = {1, 2, 3, 3}; + assert(backtracking::Subsets::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}; // input array - assert(backtracking::Subsets::subset_sum(1, array3) == - 4); // first argument in subset_sum function is the required sum and - // second is the input array + std::vector array3 = {1, 1, 1, 1}; + assert(backtracking::Subsets::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}; // input array - assert(backtracking::Subsets::subset_sum(6, array4) == - 6); // first argument in subset_sum function is the required sum and - // second is the input array + std::vector array4 = {3, 3, 3, 3}; + assert(backtracking::Subsets::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)} + std::cout << "passed" << std::endl; + + // Test 5 + std::cout << "5th test "; + std::vector array5 = {}; + assert(backtracking::Subsets::subset_sum(6, array5) == 0); // here we are expecting 0 subsets which sum up to 6 i.e. we can select anything from an empty array std::cout << "passed" << std::endl; }