Skip to content

Commit 2405a14

Browse files
authored
Update subset_sum.cpp
1 parent dc138d7 commit 2405a14

File tree

1 file changed

+11
-16
lines changed

1 file changed

+11
-16
lines changed

backtracking/subset_sum.cpp

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@
2020
namespace backtracking {
2121
/**
2222
* @namespace Subsets
23-
* @brief Functions for the [Subset
24-
* Sum](https://en.wikipedia.org/wiki/Subset_sum_problem) problem.
23+
* @brief Functions for the [Subset Sum](https://en.wikipedia.org/wiki/Subset_sum_problem) problem.
2524
*/
2625
namespace subset_sum {
2726
/**
@@ -31,7 +30,7 @@ namespace subset_sum {
3130
* @returns count of the number of subsets with required sum
3231
*/
3332

34-
uint64_t subset_sum(int64_t sum, const std::vector<int64_t> &in_arr) {
33+
uint64_t subset_sum(int sum, const std::vector<int> &in_arr) {
3534
int nelement = in_arr.size();
3635
int count_of_subset = 0;
3736

@@ -58,35 +57,31 @@ uint64_t subset_sum(int64_t sum, const std::vector<int64_t> &in_arr) {
5857
static void test() {
5958
// Test 1
6059
std::cout << "1st test ";
61-
std::vector<int64_t> array1 = {-7, -3, -2, 5, 8}; // input array
62-
assert(backtracking::subset_sum::subset_sum(0, array1) == 2); // first argument in subset_sum function is the required sum and
63-
// second is the input array
60+
std::vector<int> array1 = {-7, -3, -2, 5, 8}; // input array
61+
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
6462
std::cout << "passed" << std::endl;
6563

6664
// Test 2
6765
std::cout << "2nd test ";
68-
std::vector<uint64_t> array2 = {1, 2, 3, 3};
69-
assert(backtracking::subset_sum::subset_sum(6, array2) == 3); // here we are expecting 3 subsets which sum up to 6 i.e.
70-
// {(1,2,3),(1,2,3),(3,3)}
66+
std::vector<int> array2 = {1, 2, 3, 3};
67+
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)}
7168
std::cout << "passed" << std::endl;
7269

7370
// Test 3
7471
std::cout << "3rd test ";
75-
std::vector<uint64_t> array3 = {1, 1, 1, 1};
76-
assert(backtracking::subset_sum::subset_sum(1, array3) == 4); // here we are expecting 4 subsets which sum up to 1 i.e.
77-
// {(1),(1),(1),(1)}
72+
std::vector<int> array3 = {1, 1, 1, 1};
73+
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)}
7874
std::cout << "passed" << std::endl;
7975

8076
// Test 4
8177
std::cout << "4th test ";
82-
std::vector<uint64_t> array4 = {3, 3, 3, 3};
83-
assert(backtracking::subset_sum::subset_sum(6, array4) == 6); // here we are expecting 6 subsets which sum up to 6 i.e.
84-
// {(3,3),(3,3),(3,3),(3,3),(3,3),(3,3)}
78+
std::vector<int> array4 = {3, 3, 3, 3};
79+
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)}
8580
std::cout << "passed" << std::endl;
8681

8782
// Test 5
8883
std::cout << "5th test ";
89-
std::vector<uint64_t> array5 = {};
84+
std::vector<int> array5 = {};
9085
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
9186
std::cout << "passed" << std::endl;
9287
}

0 commit comments

Comments
 (0)