Skip to content

Commit 2325e16

Browse files
authored
Update subset_sum.cpp
1 parent 759305c commit 2325e16

File tree

1 file changed

+8
-19
lines changed

1 file changed

+8
-19
lines changed

backtracking/subset_sum.cpp

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* @file
3-
* @brief Program to Count number of subsets
3+
* @brief Program to Count number of subsets(both continuous and non-continuous subarrays) with a given sum
44
*
55
* @details Subset problem (https://en.wikipedia.org/wiki/Subset_sum_problem)
66
* @author [Swastika Gupta](https://github.com/swastyy)
@@ -17,9 +17,8 @@
1717
namespace backtracking {
1818
/**
1919
* @namespace Subsets
20-
* @brief Functions for counting subsets in a given array with a given sum
21-
* Time Complexity: O(n * 2^n), where ‘n’ is the number of elements in the given
22-
* array.
20+
* @brief Functions for counting subsets(both continuous and non-continuous subarrays) in a given array with a given sum
21+
* Time Complexity: O(n * 2^n), where ‘n’ is the number of elements in the given array.
2322
*/
2423
namespace Subsets {
2524
/**
@@ -57,41 +56,31 @@ static void test() {
5756
// Test 1
5857
std::cout << "1st test ";
5958
std::vector<int> array1 = {-7, -3, -2, 5, 8}; // input array
60-
assert(backtracking::Subsets::subset_sum(0, array1) ==
61-
2); // first argument in subset_sum function is the required sum and
62-
// second is the input array
59+
assert(backtracking::Subsets::subset_sum(0, array1) == 2); // first argument in subset_sum function is the required sum and second is the input array
6360
std::cout << "passed" << std::endl;
6461

6562
// Test 2
6663
std::cout << "2nd test ";
6764
std::vector<int> array2 = {1, 2, 3, 3};
68-
assert(backtracking::Subsets::subset_sum(6, array2) ==
69-
3); // here we are expecting 3 subsets which sum up to 6 i.e.
70-
// {(1,2,3),(1,2,3),(3,3)}
65+
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)}
7166
std::cout << "passed" << std::endl;
7267

7368
// Test 3
7469
std::cout << "3rd test ";
7570
std::vector<int> array3 = {1, 1, 1, 1};
76-
assert(backtracking::Subsets::subset_sum(1, array3) ==
77-
4); // here we are expecting 4 subsets which sum up to 1 i.e.
78-
// {(1),(1),(1),(1)}
71+
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)}
7972
std::cout << "passed" << std::endl;
8073

8174
// Test 4
8275
std::cout << "4th test ";
8376
std::vector<int> array4 = {3, 3, 3, 3};
84-
assert(backtracking::Subsets::subset_sum(6, array4) ==
85-
6); // here we are expecting 6 subsets which sum up to 6 i.e.
86-
// {(3,3),(3,3),(3,3),(3,3)}
77+
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)}
8778
std::cout << "passed" << std::endl;
8879

8980
// Test 5
9081
std::cout << "5th test ";
9182
std::vector<int> array5 = {};
92-
assert(backtracking::Subsets::subset_sum(6, array5) ==
93-
0); // here we are expecting 0 subsets which sum up to 6 i.e. we can
94-
// select anything from an empty array
83+
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
9584
std::cout << "passed" << std::endl;
9685
}
9786

0 commit comments

Comments
 (0)