I have an array of numbers: S= {4,5}and I want to check if this group creates sum = 13.
In this case, yes: 4 + 4 + 5 = 13
Another example: s={4,5}, sum = 6→ the no
I wrote a recursive function to solve this problem:
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 only works for 2 numbers in an array. I need to write a recursive function that will process N numbers, for example {4,9,3}or {3,2,1,7}etc.
I'm not sure how can I do this? How can I call recursion N times, according to the length of the array? Or maybe I should completely change my algorithm? Also - I am not allowed to use loops.