Skip to content

Commit c4e0c16

Browse files
github-actionsgithub-actions
authored andcommitted
clang-format and clang-tidy fixes for 0a293ec
1 parent d6f5e44 commit c4e0c16

File tree

2 files changed

+183
-128
lines changed

2 files changed

+183
-128
lines changed

backtracking/subarray_sum.cpp

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
11
/**
22
* @file
33
* @brief We are given with an array and a sum value. The algorithms find all
4-
* the subarrays of that array with sum equal to given sum and return such subarrays
5-
* count. This approach will have \f$O(n)\f$ time complexity and \f$O(n)\f$ space complexity.
6-
* NOTE: In this problem, we are only refering to the continuous subsets as subarrays everywhere. Subarrays can be created using deletion operation at the end or the front of an array only. The parent array is also counted in subarrays having 0 number of deletion operations.
7-
* @details Subset sum(only continuous subsets) problem (https://en.wikipedia.org/wiki/Subset_sum_problem)
4+
* the subarrays of that array with sum equal to given sum and return such
5+
* subarrays count. This approach will have \f$O(n)\f$ time complexity and
6+
* \f$O(n)\f$ space complexity. NOTE: In this problem, we are only refering to
7+
* the continuous subsets as subarrays everywhere. Subarrays can be created
8+
* using deletion operation at the end or the front of an array only. The parent
9+
* array is also counted in subarrays having 0 number of deletion operations.
10+
* @details Subset sum(only continuous subsets) problem
11+
* (https://en.wikipedia.org/wiki/Subset_sum_problem)
812
* @author [Swastika Gupta](https://github.com/swastyy)
913
*/
1014

1115
#include <cassert> /// for assert
1216
#include <iostream> /// for io operations
13-
#include <vector> /// for std::vector
1417
#include <unordered_map> /// for unordered_map
18+
#include <vector> /// for std::vector
1519

1620
/**
1721
* @namespace backtracking
@@ -20,8 +24,9 @@
2024
namespace backtracking {
2125
/**
2226
* @namespace Subarrays
23-
* @brief Functions for counting subsets(only continuous subarrays) in a given array with a given sum Time Complexity: O(n),
24-
* where ‘n’ is the number of elements in the given array.
27+
* @brief Functions for counting subsets(only continuous subarrays) in a given
28+
* array with a given sum Time Complexity: O(n), where ‘n’ is the number of
29+
* elements in the given array.
2530
*/
2631
namespace Subarrays {
2732
/**
@@ -35,11 +40,12 @@ std::uint64_t subarray_sum(int sum, const std::vector<int> &in_arr) {
3540
int nelement = in_arr.size();
3641
int count_of_subset = 0;
3742
int current_sum = 0;
38-
std::unordered_map<int, int> sumarray; // to store the subarrays count frequency having some sum value
43+
std::unordered_map<int, int> sumarray; // to store the subarrays count
44+
// frequency having some sum value
3945

4046
for (int i = 0; i < nelement; i++) {
4147
current_sum += in_arr[i];
42-
48+
4349
if (current_sum == sum) {
4450
count_of_subset++;
4551
}
@@ -62,31 +68,42 @@ static void test() {
6268
// Test 1
6369
std::cout << "1st test ";
6470
std::vector<int> array1 = {-7, -3, -2, 5, 8}; // input array
65-
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)}
71+
assert(
72+
backtracking::Subarrays::subarray_sum(0, array1) ==
73+
1); // first argument in subarray_sum function is the required sum and
74+
// second is the input array, answer is the subarray {(-3,-2,5)}
6675
std::cout << "passed" << std::endl;
6776

6877
// Test 2
6978
std::cout << "2nd test ";
7079
std::vector<int> array2 = {1, 2, 3, 3};
71-
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)}
80+
assert(backtracking::Subarrays::subarray_sum(6, array2) ==
81+
2); // here we are expecting 2 subsets which sum up to 6 i.e.
82+
// {(1,2,3),(3,3)}
7283
std::cout << "passed" << std::endl;
7384

7485
// Test 3
7586
std::cout << "3rd test ";
7687
std::vector<int> array3 = {1, 1, 1, 1};
77-
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)}
88+
assert(backtracking::Subarrays::subarray_sum(1, array3) ==
89+
4); // here we are expecting 4 subsets which sum up to 1 i.e.
90+
// {(1),(1),(1),(1)}
7891
std::cout << "passed" << std::endl;
7992

8093
// Test 4
8194
std::cout << "4th test ";
8295
std::vector<int> array4 = {3, 3, 3, 3};
83-
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)}
96+
assert(backtracking::Subarrays::subarray_sum(6, array4) ==
97+
3); // here we are expecting 3 subsets which sum up to 6 i.e.
98+
// {(3,3),(3,3),(3,3)}
8499
std::cout << "passed" << std::endl;
85100

86101
// Test 5
87102
std::cout << "5th test ";
88103
std::vector<int> array5 = {};
89-
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
104+
assert(backtracking::Subarrays::subarray_sum(6, array5) ==
105+
0); // here we are expecting 0 subsets which sum up to 6 i.e. we
106+
// cannot select anything from an empty array
90107
std::cout << "passed" << std::endl;
91108
}
92109

0 commit comments

Comments
 (0)