how to call a recursion call N number of times, given the number N?

Viewed 3377

I have an array of numbers: S= {4,5} and I want to check if this group creates the sum = 13.

In this case, yes: 4 + 4 + 5 = 13

Another example: s={4,5}, sum = 6 -> no

I wrote a recursive function to solve this:

public static boolean isSumOf(int [] s,int n)
    {
        if(n == 0)
            return true;
        if(n < 0)
            return false;

        return isSumOf(s,n-s[0]) || isSumOf(s,n-s[1]);
    }

But this function works only for 2 numbers in the array. I need to write a recursive function that will deal with N numbers, like {4,9,3} or {3,2,1,7} etc.

I'm not sure how can I do this? How can I call a recursion N times, according to the length of the array? Or maybe I should change my algorithm completely? Also - I'm not allowed to use loops.

4 Answers
Related