Skip to content

Commit 0a293ec

Browse files
authored
Create subarray_sum.cpp
1 parent 0c5c096 commit 0a293ec

File tree

1 file changed

+100
-0
lines changed

1 file changed

+100
-0
lines changed

backtracking/subarray_sum.cpp

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/**
2+
* @file
3+
* @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)
8+
* @author [Swastika Gupta](https://github.com/swastyy)
9+
*/
10+
11+
#include <cassert> /// for assert
12+
#include <iostream> /// for io operations
13+
#include <vector> /// for std::vector
14+
#include <unordered_map> /// for unordered_map
15+
16+
/**
17+
* @namespace backtracking
18+
* @brief subarray sum algorithm
19+
*/
20+
namespace backtracking {
21+
/**
22+
* @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.
25+
*/
26+
namespace Subarrays {
27+
/**
28+
* @brief The main function implements count of subarrays
29+
* @param sum is the required sum of any subarrays
30+
* @param in_arr is the input array
31+
* @returns count of the number of subsets with required sum
32+
*/
33+
34+
std::uint64_t subarray_sum(int sum, const std::vector<int> &in_arr) {
35+
int nelement = in_arr.size();
36+
int count_of_subset = 0;
37+
int current_sum = 0;
38+
std::unordered_map<int, int> sumarray; // to store the subarrays count frequency having some sum value
39+
40+
for (int i = 0; i < nelement; i++) {
41+
current_sum += in_arr[i];
42+
43+
if (current_sum == sum) {
44+
count_of_subset++;
45+
}
46+
// If in case current_sum is greater than the required sum
47+
if (sumarray.find(current_sum - sum) != sumarray.end()) {
48+
count_of_subset += (sumarray[current_sum - sum]);
49+
}
50+
sumarray[current_sum]++;
51+
}
52+
return count_of_subset;
53+
}
54+
} // namespace Subarrays
55+
} // namespace backtracking
56+
57+
/**
58+
* @brief Test implementations
59+
* @returns void
60+
*/
61+
static void test() {
62+
// Test 1
63+
std::cout << "1st test ";
64+
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)}
66+
std::cout << "passed" << std::endl;
67+
68+
// Test 2
69+
std::cout << "2nd test ";
70+
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)}
72+
std::cout << "passed" << std::endl;
73+
74+
// Test 3
75+
std::cout << "3rd test ";
76+
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)}
78+
std::cout << "passed" << std::endl;
79+
80+
// Test 4
81+
std::cout << "4th test ";
82+
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)}
84+
std::cout << "passed" << std::endl;
85+
86+
// Test 5
87+
std::cout << "5th test ";
88+
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
90+
std::cout << "passed" << std::endl;
91+
}
92+
93+
/**
94+
* @brief Main function
95+
* @returns 0 on exit
96+
*/
97+
int main() {
98+
test(); // execute the test
99+
return 0;
100+
}

0 commit comments

Comments
 (0)