#include
using namespace std;
int s(int *a,int i,int sum,int n){
if(i==n){
if(sum==0){
return 1;
}
return 0;
}
s(a,i+1,sum+a[i],n);
s(a,i+1,sum,n);
}
int main(){
int t;
cin>>t;
for(int i=1;i<=t;i++){
int n;
cin>>n;
int a[n];
for(int i=0;i<n;i++){
cin>>a[i];
}
if(s(a,0,0,n)){
cout<<“Yes”<<endl;
}
else{
cout<<“No”<<endl;
}
}
return 0;
}
why my code is not working
Problem in code
hi @professor
- Make
sumas long long int because it may have to store large values - You are not returning anything in the recursive case. The answer returned from the base case needs to be relayed back to the main function.
- In your base case, you need to take care of the case of empty subset. Right now, if subset is empty, it will also return true, but it should return false. For that you can use a count variable, that keeps track of the number of elements being used for “sum”
- Datatype of
s()should be bool. It will not produce error if you use int, but technically it should be bool only.
You can see the changes here
Please mark the doubt as resolved in case of no further queries.