Computing a number of Sub Sets Recursively

So, I have a program here that, when given an array, its size, and a target number, it calculates the number of integer combinations (sub sets) that will produce the target number.

Example: an int array {3, 7, 1, 8, -3}, size of 5, with target number 4, will return 2. (3+1=4 and 7+(-3)=4)

My question is: How could I rewrite this function so that it is a Recursive function? (I'm new to recursion and am trying to understand it) Any help would be much appreciated! Thank you!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
int CountSubSetSum(int ArrSet[], int Size, int Sum)
{
    int count = 0;
    for (int i = 0; i <= Size; i++)
    {
        for (int j = Size; j > i; j--)
        {
            if (ArrSet[i] + ArrSet[j] == Sum)
            {
                count++;
            }
        }

        if (ArrSet[i] == Sum)
        {
            count++;
        }
    }

    return count;
}
Last edited on
Topic archived. No new replies allowed.