From 9b0b5f874d82ab4064e8007f73015d27a38f8c48 Mon Sep 17 00:00:00 2001 From: Swastika Gupta <64654203+Swastyy@users.noreply.github.com> Date: Sat, 10 Jul 2021 13:12:43 +0530 Subject: [PATCH] Update subarray_sum.cpp --- backtracking/subarray_sum.cpp | 34 +++++++++++----------------------- 1 file changed, 11 insertions(+), 23 deletions(-) diff --git a/backtracking/subarray_sum.cpp b/backtracking/subarray_sum.cpp index bfd9ae27b..29d9fd665 100644 --- a/backtracking/subarray_sum.cpp +++ b/backtracking/subarray_sum.cpp @@ -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 /// 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 &in_arr) { +uint64_t subarray_sum(int sum, const std::vector &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 &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 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 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 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 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 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; }