Algorithms_in_C++ 1.0.0
Set of algorithms implemented in C++.
Loading...
Searching...
No Matches
subarray_sum.cpp File Reference

Subset-sum (only continuous subsets) problem More...

#include <cassert>
#include <iostream>
#include <unordered_map>
#include <vector>
Include dependency graph for subarray_sum.cpp:

Namespaces

namespace  backtracking
 for vector container
 
namespace  subarray_sum
 Functions for the Subset sum implementation.
 

Functions

uint64_t backtracking::subarray_sum::subarray_sum (int64_t sum, const std::vector< int64_t > &in_arr)
 The main function that implements the count of the subarrays.
 
static void test ()
 Self-test implementations.
 
int main ()
 Main function.
 

Detailed Description

Subset-sum (only continuous subsets) problem

We are given an array and a sum value. The algorithms find all the subarrays of that array with sum equal to the given sum and return such subarrays count. This approach will have \(O(n)\) time complexity and \(O(n)\) space complexity. NOTE: In this problem, we are only referring to the continuous subsets as subarrays everywhere. Subarrays can be created using deletion operation at the end of the front of an array only. The parent array is also counted in subarrays having 0 number of deletion operations.

Author
Swastika Gupta

Function Documentation

◆ main()

int main ( void )

Main function.

Returns
0 on exit
114 {
115 test(); // run self-test implementations
116 return 0;
117}
static void test()
Self-test implementations.
Definition subarray_sum.cpp:67
Here is the call graph for this function:

◆ subarray_sum()

uint64_t backtracking::subarray_sum::subarray_sum ( int64_t sum,
const std::vector< int64_t > & in_arr )

The main function that implements the count of the subarrays.

Parameters
sumis the required sum of any subarrays
in_arris the input array
Returns
count of the number of subsets with required sum
38 {
39 int64_t nelement = in_arr.size();
40 int64_t count_of_subset = 0;
41 int64_t current_sum = 0;
43 sumarray; // to store the subarrays count
44 // frequency having some sum value
45
46 for (int64_t i = 0; i < nelement; i++) {
47 current_sum += in_arr[i];
48
49 if (current_sum == sum) {
50 count_of_subset++;
51 }
52 // If in case current_sum is greater than the required sum
53 if (sumarray.find(current_sum - sum) != sumarray.end()) {
54 count_of_subset += (sumarray[current_sum - sum]);
55 }
56 sumarray[current_sum]++;
57 }
58 return count_of_subset;
59}
T end(T... args)
T find(T... args)
T sum(const std::vector< std::valarray< T > > &A)
Definition vector_ops.hpp:232
T size(T... args)
Here is the call graph for this function:

◆ test()

static void test ( )
static

Self-test implementations.

Returns
void
67 {
68 // 1st test
69 std::cout << "1st test ";
70 std::vector<int64_t> array1 = {-7, -3, -2, 5, 8}; // input array
71 assert(
72 backtracking::subarray_sum::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)}
75 std::cout << "passed" << std::endl;
76
77 // 2nd test
78 std::cout << "2nd test ";
79 std::vector<int64_t> array2 = {1, 2, 3, 3};
80 assert(backtracking::subarray_sum::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)}
83 std::cout << "passed" << std::endl;
84
85 // 3rd test
86 std::cout << "3rd test ";
87 std::vector<int64_t> array3 = {1, 1, 1, 1};
88 assert(backtracking::subarray_sum::subarray_sum(1, array3) ==
89 4); // here we are expecting 4 subsets which sum up to 1 i.e.
90 // {(1),(1),(1),(1)}
91 std::cout << "passed" << std::endl;
92
93 // 4rd test
94 std::cout << "4th test ";
95 std::vector<int64_t> array4 = {3, 3, 3, 3};
96 assert(backtracking::subarray_sum::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)}
99 std::cout << "passed" << std::endl;
100
101 // 5th test
102 std::cout << "5th test ";
103 std::vector<int64_t> array5 = {};
104 assert(backtracking::subarray_sum::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
107 std::cout << "passed" << std::endl;
108}
T endl(T... args)
Here is the call graph for this function: