diff --git a/backtracking/subset_sum.cpp b/backtracking/subset_sum.cpp index 6213206de..633bd8ff6 100644 --- a/backtracking/subset_sum.cpp +++ b/backtracking/subset_sum.cpp @@ -1,15 +1,15 @@ /** * @file - * @brief Program to Count number of subsets + * @brief Program to Count number of subsets * * @details Subset problem (https://en.wikipedia.org/wiki/Subset_sum_problem) * @author [Swastika Gupta](https://github.com/swastyy) */ - -#include /// for assert -#include /// for io operations -#include /// for std::vector - + +#include /// for assert +#include /// for io operations +#include /// for std::vector + /** * @namespace backtracking * @brief subset sum algorithm @@ -28,25 +28,25 @@ namespace Subsets { */ std::uint64_t subset_sum(int sum, const std::vector &in_arr) { - int nelement = in_arr.size(); //number of subset element - int count_of_subset = 0; + int nelement = in_arr.size(); // number of subset element + int count_of_subset = 0; - for (int i=0; i < (1 << (nelement)); i++) { - std::vector subset; - for ( int j=0 ; j < nelement ; j++) { - if (i & (1< subset; + for (int j = 0; j < nelement; j++) { + if (i & (1 << j)) { + subset.push_back(in_arr[j]); + } + } + int check = 0; + for (int k : subset) { + check += k; + } + if (check == sum) { + count_of_subset++; + } + } + return count_of_subset; } } // namespace Subsets } // namespace backtracking @@ -58,30 +58,37 @@ 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}; // 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::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}; // 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::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}; // 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::cout << "passed" << std::endl; } - /** * @brief Main function * @returns 0 on exit