9
9
* array is also counted in subarrays having 0 number of deletion operations.
10
10
* @details Subset sum(only continuous subsets) problem
11
11
* (https://en.wikipedia.org/wiki/Subset_sum_problem)
12
- * @author [Swastika Gupta](https://github.com/swastyy )
12
+ * @author [Swastika Gupta](https://github.com/Swastyy )
13
13
*/
14
14
15
15
#include < cassert> // / for assert
23
23
*/
24
24
namespace backtracking {
25
25
/* *
26
- * @namespace subarray_sum
27
- * @brief Functions for the [Subset
28
- * sum](https://en.wikipedia.org/wiki/Subset_sum_problem) implementation
26
+ * @namespace subarraySum
27
+ * @brief Functions for the [Subset sum](https://en.wikipedia.org/wiki/Subset_sum_problem) implementation
29
28
*/
30
- namespace Subarrays {
29
+ namespace subarraySum {
31
30
/* *
32
31
* @brief The main function implements count of subarrays
33
32
* @param sum is the required sum of any subarrays
34
33
* @param in_arr is the input array
35
34
* @returns count of the number of subsets with required sum
36
35
*/
37
36
38
- std:: uint64_t subarray_sum (int sum, const std::vector<int > &in_arr) {
37
+ uint64_t subarray_sum (int sum, const std::vector<int > &in_arr) {
39
38
int nelement = in_arr.size ();
40
39
int count_of_subset = 0 ;
41
40
int current_sum = 0 ;
@@ -56,7 +55,7 @@ std::uint64_t subarray_sum(int sum, const std::vector<int> &in_arr) {
56
55
}
57
56
return count_of_subset;
58
57
}
59
- } // namespace Subarrays
58
+ } // namespace subarraySum
60
59
} // namespace backtracking
61
60
62
61
/* *
@@ -67,42 +66,31 @@ static void test() {
67
66
// Test 1
68
67
std::cout << " 1st test " ;
69
68
std::vector<int > array1 = {-7 , -3 , -2 , 5 , 8 }; // input array
70
- assert (
71
- backtracking::Subarrays::subarray_sum (0 , array1) ==
72
- 1 ); // first argument in subarray_sum function is the required sum and
73
- // second is the input array, answer is the subarray {(-3,-2,5)}
69
+ assert (backtracking::subarraySum::subarray_sum (0 , array1) == 1 ); // first argument in subarray_sum function is the required sum and second is the input array, answer is the subarray {(-3,-2,5)}
74
70
std::cout << " passed" << std::endl;
75
71
76
72
// Test 2
77
73
std::cout << " 2nd test " ;
78
74
std::vector<int > array2 = {1 , 2 , 3 , 3 };
79
- assert (backtracking::Subarrays::subarray_sum (6 , array2) ==
80
- 2 ); // here we are expecting 2 subsets which sum up to 6 i.e.
81
- // {(1,2,3),(3,3)}
75
+ assert (backtracking::subarraySum::subarray_sum (6 , array2) == 2 ); // here we are expecting 2 subsets which sum up to 6 i.e. {(1,2,3),(3,3)}
82
76
std::cout << " passed" << std::endl;
83
77
84
78
// Test 3
85
79
std::cout << " 3rd test " ;
86
80
std::vector<int > array3 = {1 , 1 , 1 , 1 };
87
- assert (backtracking::Subarrays::subarray_sum (1 , array3) ==
88
- 4 ); // here we are expecting 4 subsets which sum up to 1 i.e.
89
- // {(1),(1),(1),(1)}
81
+ assert (backtracking::subarraySum::subarray_sum (1 , array3) == 4 ); // here we are expecting 4 subsets which sum up to 1 i.e. {(1),(1),(1),(1)}
90
82
std::cout << " passed" << std::endl;
91
83
92
84
// Test 4
93
85
std::cout << " 4th test " ;
94
86
std::vector<int > array4 = {3 , 3 , 3 , 3 };
95
- assert (backtracking::Subarrays::subarray_sum (6 , array4) ==
96
- 3 ); // here we are expecting 3 subsets which sum up to 6 i.e.
97
- // {(3,3),(3,3),(3,3)}
87
+ assert (backtracking::subarraySum::subarray_sum (6 , array4) == 3 ); // here we are expecting 3 subsets which sum up to 6 i.e. {(3,3),(3,3),(3,3)}
98
88
std::cout << " passed" << std::endl;
99
89
100
90
// Test 5
101
91
std::cout << " 5th test " ;
102
92
std::vector<int > array5 = {};
103
- assert (backtracking::Subarrays::subarray_sum (6 , array5) ==
104
- 0 ); // here we are expecting 0 subsets which sum up to 6 i.e. we
105
- // cannot select anything from an empty array
93
+ assert (backtracking::subarraySum::subarray_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
106
94
std::cout << " passed" << std::endl;
107
95
}
108
96
0 commit comments