Your teacher wants you to use recursion.
What is the answer, for a given X, if Y is zero? Solve this using your code.
What is the answer, for a given X, if I give you the solution for Y = some random whole number n for free, what is the solution for n + 1? In other words, if I tell you that the solution for X = 5, Y = 3 is { { ... }, { ... }, ... }, can you easily figure out the solution for X = 5, Y = 3 + 1 = 4?
Here is an example for a totally different problem:
Lets say you know the first previous two Fibonacci numbers are 1 and 1. Then finding the next one is easy, right? it's 2. Now lets say you know the previous two are 1 and 2, the next one is 3! If the previous two are 2 and 3, the next one is 5!
Some pseudocode:
public int fib(int stop) {
if (stop < 2) return 1;
return fibHelp(stop - 2, 1, 1);
}
public int fibHelp(int stop, int oneBelow, int twoBelow) {
if (stop == 0) return oneBelow;
return fibHelp(stop - 1, oneBelow + twoBelow, oneBelow);
}
See how fibHelp calls itself? That's recursion! Just make sure you have a stop condition (my if statement).
For your specific problem, don't return void
, instead have comboNoRep
return a Set<Set<Integer>>
. When y=0
, return a Set
with one element (an empty Set
). y=1
, return a Set
that builds a bunch of Set
s by adding one element to each set in the larger set (in the case of y=1
that Set
is empty, and so forth).
Use Set
and not List
because you want to make sure that you don't have duplicates.