|
1 | 1 | /**
|
2 | 2 | * @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 |
4 | 4 | *
|
5 | 5 | * @details Subset problem (https://en.wikipedia.org/wiki/Subset_sum_problem)
|
6 | 6 | * @author [Swastika Gupta](https://github.com/swastyy)
|
|
17 | 17 | namespace backtracking {
|
18 | 18 | /**
|
19 | 19 | * @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. |
23 | 22 | */
|
24 | 23 | namespace Subsets {
|
25 | 24 | /**
|
@@ -57,41 +56,31 @@ static void test() {
|
57 | 56 | // Test 1
|
58 | 57 | std::cout << "1st test ";
|
59 | 58 | 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 |
63 | 60 | std::cout << "passed" << std::endl;
|
64 | 61 |
|
65 | 62 | // Test 2
|
66 | 63 | std::cout << "2nd test ";
|
67 | 64 | 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)} |
71 | 66 | std::cout << "passed" << std::endl;
|
72 | 67 |
|
73 | 68 | // Test 3
|
74 | 69 | std::cout << "3rd test ";
|
75 | 70 | 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)} |
79 | 72 | std::cout << "passed" << std::endl;
|
80 | 73 |
|
81 | 74 | // Test 4
|
82 | 75 | std::cout << "4th test ";
|
83 | 76 | 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)} |
87 | 78 | std::cout << "passed" << std::endl;
|
88 | 79 |
|
89 | 80 | // Test 5
|
90 | 81 | std::cout << "5th test ";
|
91 | 82 | 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 |
95 | 84 | std::cout << "passed" << std::endl;
|
96 | 85 | }
|
97 | 86 |
|
|
0 commit comments