Gives Wrong answer when added 0 to array

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]);

}} } }

@neelesh_garg,
It goes into an infinite loop at this point:
{ if (tq>=box[i])

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

Since, tq won’t change in case of 0. Hence we keep on adding 0 again and again. Hence no answer

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.