Update subarray_sum.cpp

This commit is contained in:
Swastika Gupta 2021-07-10 13:12:43 +05:30 committed by GitHub
parent 8318cc3e92
commit 9b0b5f874d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -9,7 +9,7 @@
* array is also counted in subarrays having 0 number of deletion operations.
* @details Subset sum(only continuous subsets) problem
* (https://en.wikipedia.org/wiki/Subset_sum_problem)
* @author [Swastika Gupta](https://github.com/swastyy)
* @author [Swastika Gupta](https://github.com/Swastyy)
*/
#include <cassert> /// for assert
@ -23,11 +23,10 @@
*/
namespace backtracking {
/**
* @namespace subarray_sum
* @brief Functions for the [Subset
* sum](https://en.wikipedia.org/wiki/Subset_sum_problem) implementation
* @namespace subarraySum
* @brief Functions for the [Subset sum](https://en.wikipedia.org/wiki/Subset_sum_problem) implementation
*/
namespace Subarrays {
namespace subarraySum {
/**
* @brief The main function implements count of subarrays
* @param sum is the required sum of any subarrays
@ -35,7 +34,7 @@ namespace Subarrays {
* @returns count of the number of subsets with required sum
*/
std::uint64_t subarray_sum(int sum, const std::vector<int> &in_arr) {
uint64_t subarray_sum(int sum, const std::vector<int> &in_arr) {
int nelement = in_arr.size();
int count_of_subset = 0;
int current_sum = 0;
@ -56,7 +55,7 @@ std::uint64_t subarray_sum(int sum, const std::vector<int> &in_arr) {
}
return count_of_subset;
}
} // namespace Subarrays
} // namespace subarraySum
} // namespace backtracking
/**
@ -67,42 +66,31 @@ static void test() {
// Test 1
std::cout << "1st test ";
std::vector<int> array1 = {-7, -3, -2, 5, 8}; // input array
assert(
backtracking::Subarrays::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)}
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)}
std::cout << "passed" << std::endl;
// Test 2
std::cout << "2nd test ";
std::vector<int> array2 = {1, 2, 3, 3};
assert(backtracking::Subarrays::subarray_sum(6, array2) ==
2); // here we are expecting 2 subsets which sum up to 6 i.e.
// {(1,2,3),(3,3)}
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)}
std::cout << "passed" << std::endl;
// Test 3
std::cout << "3rd test ";
std::vector<int> array3 = {1, 1, 1, 1};
assert(backtracking::Subarrays::subarray_sum(1, array3) ==
4); // here we are expecting 4 subsets which sum up to 1 i.e.
// {(1),(1),(1),(1)}
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)}
std::cout << "passed" << std::endl;
// Test 4
std::cout << "4th test ";
std::vector<int> array4 = {3, 3, 3, 3};
assert(backtracking::Subarrays::subarray_sum(6, array4) ==
3); // here we are expecting 3 subsets which sum up to 6 i.e.
// {(3,3),(3,3),(3,3)}
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)}
std::cout << "passed" << std::endl;
// Test 5
std::cout << "5th test ";
std::vector<int> array5 = {};
assert(backtracking::Subarrays::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
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
std::cout << "passed" << std::endl;
}