Skip to content

Commit 9b0b5f8

Browse files
authored
Update subarray_sum.cpp
1 parent 8318cc3 commit 9b0b5f8

File tree

1 file changed

+11
-23
lines changed

1 file changed

+11
-23
lines changed

backtracking/subarray_sum.cpp

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* array is also counted in subarrays having 0 number of deletion operations.
1010
* @details Subset sum(only continuous subsets) problem
1111
* (https://en.wikipedia.org/wiki/Subset_sum_problem)
12-
* @author [Swastika Gupta](https://github.com/swastyy)
12+
* @author [Swastika Gupta](https://github.com/Swastyy)
1313
*/
1414

1515
#include <cassert> /// for assert
@@ -23,19 +23,18 @@
2323
*/
2424
namespace backtracking {
2525
/**
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
2928
*/
30-
namespace Subarrays {
29+
namespace subarraySum {
3130
/**
3231
* @brief The main function implements count of subarrays
3332
* @param sum is the required sum of any subarrays
3433
* @param in_arr is the input array
3534
* @returns count of the number of subsets with required sum
3635
*/
3736

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) {
3938
int nelement = in_arr.size();
4039
int count_of_subset = 0;
4140
int current_sum = 0;
@@ -56,7 +55,7 @@ std::uint64_t subarray_sum(int sum, const std::vector<int> &in_arr) {
5655
}
5756
return count_of_subset;
5857
}
59-
} // namespace Subarrays
58+
} // namespace subarraySum
6059
} // namespace backtracking
6160

6261
/**
@@ -67,42 +66,31 @@ static void test() {
6766
// Test 1
6867
std::cout << "1st test ";
6968
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)}
7470
std::cout << "passed" << std::endl;
7571

7672
// Test 2
7773
std::cout << "2nd test ";
7874
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)}
8276
std::cout << "passed" << std::endl;
8377

8478
// Test 3
8579
std::cout << "3rd test ";
8680
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)}
9082
std::cout << "passed" << std::endl;
9183

9284
// Test 4
9385
std::cout << "4th test ";
9486
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)}
9888
std::cout << "passed" << std::endl;
9989

10090
// Test 5
10191
std::cout << "5th test ";
10292
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
10694
std::cout << "passed" << std::endl;
10795
}
10896

0 commit comments

Comments
 (0)