Update subset_sum.cpp

This commit is contained in:
Swastika Gupta 2021-07-05 06:17:06 +05:30 committed by GitHub
parent 759305cb6b
commit 2325e16551
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,6 +1,6 @@
/**
* @file
* @brief Program to Count number of subsets
* @brief Program to Count number of subsets(both continuous and non-continuous subarrays) with a given sum
*
* @details Subset problem (https://en.wikipedia.org/wiki/Subset_sum_problem)
* @author [Swastika Gupta](https://github.com/swastyy)
@ -17,9 +17,8 @@
namespace backtracking {
/**
* @namespace Subsets
* @brief Functions for counting subsets in a given array with a given sum
* Time Complexity: O(n * 2^n), where n is the number of elements in the given
* array.
* @brief Functions for counting subsets(both continuous and non-continuous subarrays) in a given array with a given sum
* Time Complexity: O(n * 2^n), where n is the number of elements in the given array.
*/
namespace Subsets {
/**
@ -57,41 +56,31 @@ static void test() {
// Test 1
std::cout << "1st test ";
std::vector<int> 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
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<int> 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)}
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<int> 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)}
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<int> 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)}
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),(3,3),(3,3)}
std::cout << "passed" << std::endl;
// Test 5
std::cout << "5th test ";
std::vector<int> 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
assert(backtracking::Subsets::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;
}