Subset sum possible or not

plz check this code is showing wrong ans …we have to print yes or no if we can obtain a given sum by choosing a subset of given array or not…
plz correct this code and explain as well…

@S19LPPP0202

bool subset(int n,int a[],int x,int i){
if(x==0){ //this would come first
    return true;
}

if(i==n){
    return false;
}

if(x<0){
    return false;
}

bool flag=subset(n,a,x-a[i],i+1)||subset(n,a,x,i+1); // don't do ++i , as it changes the value of i in second case as well.
return flag;

}
Note: you have to memoize this also.

why will x==0 come first??plz explain…

will this code work for negative numbers in an array as well??

because if i==n and x was zero then you will return false, but x = 0 then you have to return true.
yes it will work for negative number.

plz reply to this doubt of mine as well.this is unresolved…

click on this link and plz reply to this doubt of mine as well…
it would be really helpful

Only you can understand your approach better.
and also your approach, may not work for all cases.
Try to think/debug yourself only that will help you grow and improve.

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.