You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: backtracking/subarray_sum.cpp
+31-14Lines changed: 31 additions & 14 deletions
Original file line number
Diff line number
Diff line change
@@ -1,17 +1,21 @@
1
1
/**
2
2
* @file
3
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)
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
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)}
3); // here we are expecting 3 subsets which sum up to 6 i.e.
98
+
// {(3,3),(3,3),(3,3)}
84
99
std::cout << "passed" << std::endl;
85
100
86
101
// Test 5
87
102
std::cout << "5th test ";
88
103
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
0 commit comments