uint64_t
subarray_sum(int64_t sum,
conststd::vector<int64_t> &in_arr) {
40int64_t nelement = in_arr.size();
41int64_t count_of_subset = 0;
42int64_t current_sum = 0;
43std::unordered_map<int64_t, int64_t>
47 for(int64_t i = 0; i < nelement; i++) {
48current_sum += in_arr[i];
50 if(current_sum == sum) {
54 if(sumarray.find(current_sum - sum) != sumarray.end()) {
55count_of_subset += (sumarray[current_sum - sum]);
57sumarray[current_sum]++;
59 returncount_of_subset;
70std::cout <<
"1st test ";
71std::vector<int64_t> array1 = {-7, -3, -2, 5, 8};
76std::cout <<
"passed"<< std::endl;
79std::cout <<
"2nd test ";
80std::vector<int64_t> array2 = {1, 2, 3, 3};
84std::cout <<
"passed"<< std::endl;
87std::cout <<
"3rd test ";
88std::vector<int64_t> array3 = {1, 1, 1, 1};
92std::cout <<
"passed"<< std::endl;
95std::cout <<
"4th test ";
96std::vector<int64_t> array4 = {3, 3, 3, 3};
100std::cout <<
"passed"<< std::endl;
103std::cout <<
"5th test ";
104std::vector<int64_t> array5 = {};
108std::cout <<
"passed"<< std::endl;
Functions for the Subset sum implementation.
static void test()
Self-test implementations.
int main()
Main function.
uint64_t subarray_sum(int64_t sum, const std::vector< int64_t > &in_arr)
The main function that implements the count of the subarrays.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4