Update subset_sum.cpp

This commit is contained in:
Swastika Gupta 2021-07-11 17:32:26 +05:30 committed by GitHub
parent 24c901c9a5
commit 383baeb305
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -31,13 +31,13 @@ namespace subset_sum {
* @returns count of the number of subsets with required sum * @returns count of the number of subsets with required sum
*/ */
uint64_t subset_sum(int sum, const std::vector<int> &in_arr) { uint64_t subset_sum(int32_t sum, const std::vector<int32_t> &in_arr) {
int nelement = in_arr.size(); int32_t nelement = in_arr.size();
int count_of_subset = 0; uint64_t count_of_subset = 0;
for (int i = 0; i < (1 << (nelement)); i++) { for (int32_t i = 0; i < (1 << (nelement)); i++) {
int check = 0; int32_t check = 0;
for (int j = 0; j < nelement; j++) { for (int32_t j = 0; j < nelement; j++) {
if (i & (1 << j)) { if (i & (1 << j)) {
check += (in_arr[j]); check += (in_arr[j]);
} }
@ -58,7 +58,7 @@ uint64_t subset_sum(int sum, const std::vector<int> &in_arr) {
static void test() { static void test() {
// Test 1 // Test 1
std::cout << "1st test "; std::cout << "1st test ";
std::vector<int> array1 = {-7, -3, -2, 5, 8}; // input array std::vector<int32_t> array1 = {-7, -3, -2, 5, 8}; // input array
assert(backtracking::subset_sum::subset_sum(0, array1) == assert(backtracking::subset_sum::subset_sum(0, array1) ==
2); // first argument in subset_sum function is the required sum and 2); // first argument in subset_sum function is the required sum and
// second is the input array // second is the input array
@ -66,34 +66,28 @@ static void test() {
// Test 2 // Test 2
std::cout << "2nd test "; std::cout << "2nd test ";
std::vector<int> array2 = {1, 2, 3, 3}; std::vector<int32_t> array2 = {1, 2, 3, 3};
assert(backtracking::subset_sum::subset_sum(6, array2) == assert(backtracking::subset_sum::subset_sum(6, array2) == 3); // here we are expecting 3 subsets which sum up to 6 i.e.
3); // here we are expecting 3 subsets which sum up to 6 i.e.
// {(1,2,3),(1,2,3),(3,3)} // {(1,2,3),(1,2,3),(3,3)}
std::cout << "passed" << std::endl; std::cout << "passed" << std::endl;
// Test 3 // Test 3
std::cout << "3rd test "; std::cout << "3rd test ";
std::vector<int> array3 = {1, 1, 1, 1}; std::vector<int32_t> array3 = {1, 1, 1, 1};
assert(backtracking::subset_sum::subset_sum(1, array3) == assert(backtracking::subset_sum::subset_sum(1, array3) == 4); // here we are expecting 4 subsets which sum up to 1 i.e.
4); // here we are expecting 4 subsets which sum up to 1 i.e.
// {(1),(1),(1),(1)} // {(1),(1),(1),(1)}
std::cout << "passed" << std::endl; std::cout << "passed" << std::endl;
// Test 4 // Test 4
std::cout << "4th test "; std::cout << "4th test ";
std::vector<int> array4 = {3, 3, 3, 3}; std::vector<int32_t> array4 = {3, 3, 3, 3};
assert(backtracking::subset_sum::subset_sum(6, array4) == 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)}
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; std::cout << "passed" << std::endl;
// Test 5 // Test 5
std::cout << "5th test "; std::cout << "5th test ";
std::vector<int> array5 = {}; std::vector<int32_t> array5 = {};
assert(backtracking::subset_sum::subset_sum(6, 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
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; std::cout << "passed" << std::endl;
} }