How to call recursion call N number of times given number N?

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 = 6the 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.

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

:

for (int i = 0; i < s.length; ++i) {
  if (isSumOf(s,n-s[i])) return true;
}
return false;

, , :

boolean withoutLoop(int [] s,int n, int i) {
  if (i >= s.length) return false;
  return isSumOf(s,n-s[i]) || recurse(s, n, i+1);
}

, isSumOf:

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

    return withoutLoop(s, n, 0);  // Change here.
}

, :

return (n == 0) || (n < 0 && withoutLoop(s, n, 0));
+4

:

  • , 13?

; 1 N 1 + 2 N.

, / .

0

:

ResultType recursiveMethod(params) {
     if( /* this is the simplest case */ ) {
         return answer for the simplest case
     } else {
         partialResult = solve part of the problem
         resultForRest = recursiveMethod(rest of problem)
     }
}

, :

if(list is empty) {
    return solution for an empty list
} else {
    r = call self recursively for tail of list
    return solution for head of list combined with r
}

( "" - , "" - . .)

:

 if(s.length == 0) {
      return n == 0;
 }

else " " s[0], " " - s[1] .

 ... 
 } else {
     int head = s[0];
     int[] tail = Arrays.copyOfRange(s,1,s.length-1);
     return isSumOf(tail, n - head);
 }

The code will be cleaner (and probably more efficient) if you used Listinstead of an array directly, because instead copyOfRange()you could use List.subList().

You can also pass the entire array every time, along with an additional parameter indicating how much of the array has already been taken into account.

0
source

This should work:

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

        for (int x: s) {
            if (isSumOf(s, n-x)) {
                return true;
            }
        }
        return false;
    }

UPDATE:

ABOUT! no loop, only recursion, you will need an additional argument:

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

        return isSum2(s, n, 0);
    }

public static boolean isSum2(int [] s,int n,int i)
    {
        if (i >= s.length)
            return false;

        return isSumOf(s,n-s[i]) || isSum2(s,n,i+1);
    }
-1
source

Source: https://habr.com/ru/post/1677674/


All Articles