Coin change combination

if i add 0 to array we are passing why it doesn’t it work and i remove the 0 from the same array it works fine

public class Coin {

static int count=0;

public static void main(String[] args){

    Qc(new int[]{2,3,5,6},20," ");

}
public static void Qc(int[] box,int tq,  String ans){
    if(tq==0){
        count++;
        System.out.println(count+". "+ans );
        return;
    }
    for(int i=0;i<box.length;i++)
    {   if (tq>=box[i]){

        Qc(box,tq-box[i],ans+box[i]);

    }}
}

}

if you add 0 to the array, the recursion become an infinite recursion. because the call to
Qc(box,tq,ans) will happen again and again(since box[i]=0). whenever we design any recursion, we always reduce something in next recursion call, so that the problem get reduced to base condition to stop recursion. but if you add 0, your next call will have no reduction.

Thanks

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.