Skip to content

Commit 383baeb

Browse files
authored
Update subset_sum.cpp
1 parent 24c901c commit 383baeb

File tree

1 file changed

+15
-21
lines changed

1 file changed

+15
-21
lines changed

backtracking/subset_sum.cpp

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,13 @@ namespace subset_sum {
3131
* @returns count of the number of subsets with required sum
3232
*/
3333

34-
uint64_t subset_sum(int sum, const std::vector<int> &in_arr) {
35-
int nelement = in_arr.size();
36-
int count_of_subset = 0;
34+
uint64_t subset_sum(int32_t sum, const std::vector<int32_t> &in_arr) {
35+
int32_t nelement = in_arr.size();
36+
uint64_t count_of_subset = 0;
3737

38-
for (int i = 0; i < (1 << (nelement)); i++) {
39-
int check = 0;
40-
for (int j = 0; j < nelement; j++) {
38+
for (int32_t i = 0; i < (1 << (nelement)); i++) {
39+
int32_t check = 0;
40+
for (int32_t j = 0; j < nelement; j++) {
4141
if (i & (1 << j)) {
4242
check += (in_arr[j]);
4343
}
@@ -58,42 +58,36 @@ uint64_t subset_sum(int sum, const std::vector<int> &in_arr) {
5858
static void test() {
5959
// Test 1
6060
std::cout << "1st test ";
61-
std::vector<int> array1 = {-7, -3, -2, 5, 8}; // input array
61+
std::vector<int32_t> array1 = {-7, -3, -2, 5, 8}; // input array
6262
assert(backtracking::subset_sum::subset_sum(0, array1) ==
6363
2); // first argument in subset_sum function is the required sum and
6464
// second is the input array
6565
std::cout << "passed" << std::endl;
6666

6767
// Test 2
6868
std::cout << "2nd test ";
69-
std::vector<int> array2 = {1, 2, 3, 3};
70-
assert(backtracking::subset_sum::subset_sum(6, array2) ==
71-
3); // here we are expecting 3 subsets which sum up to 6 i.e.
69+
std::vector<int32_t> array2 = {1, 2, 3, 3};
70+
assert(backtracking::subset_sum::subset_sum(6, array2) == 3); // here we are expecting 3 subsets which sum up to 6 i.e.
7271
// {(1,2,3),(1,2,3),(3,3)}
7372
std::cout << "passed" << std::endl;
7473

7574
// Test 3
7675
std::cout << "3rd test ";
77-
std::vector<int> array3 = {1, 1, 1, 1};
78-
assert(backtracking::subset_sum::subset_sum(1, array3) ==
79-
4); // here we are expecting 4 subsets which sum up to 1 i.e.
76+
std::vector<int32_t> array3 = {1, 1, 1, 1};
77+
assert(backtracking::subset_sum::subset_sum(1, array3) == 4); // here we are expecting 4 subsets which sum up to 1 i.e.
8078
// {(1),(1),(1),(1)}
8179
std::cout << "passed" << std::endl;
8280

8381
// Test 4
8482
std::cout << "4th test ";
85-
std::vector<int> array4 = {3, 3, 3, 3};
86-
assert(backtracking::subset_sum::subset_sum(6, array4) ==
87-
6); // here we are expecting 6 subsets which sum up to 6 i.e.
88-
// {(3,3),(3,3),(3,3),(3,3),(3,3),(3,3)}
83+
std::vector<int32_t> array4 = {3, 3, 3, 3};
84+
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)}
8985
std::cout << "passed" << std::endl;
9086

9187
// Test 5
9288
std::cout << "5th test ";
93-
std::vector<int> array5 = {};
94-
assert(backtracking::subset_sum::subset_sum(6, array5) ==
95-
0); // here we are expecting 0 subsets which sum up to 6 i.e. we
96-
// cannot select anything from an empty array
89+
std::vector<int32_t> array5 = {};
90+
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
9791
std::cout << "passed" << std::endl;
9892
}
9993

0 commit comments

Comments
 (0)