why do we need
final int numRemaining = k - partialCombination.size();
and
i <= n && numRemaining <= n-i+1
I tried code without these and also works fine. I just do not understand
i <= n && numRemaining <= n-i+1
why do we need
final int numRemaining = k - partialCombination.size();
and
i <= n && numRemaining <= n-i+1
I tried code without these and also works fine. I just do not understand
i <= n && numRemaining <= n-i+1
It is used for tuning because if we don’t have enough elements to pick in the remaining subarray, there is no need to continue. You can think this as a performance improvement but not for the correctness.
You actually don’t even need the i <= n condition. The second one will take care of that.