Subset Sum Easy


Cant understand what is wrong

Hello @Rishabh3699,

Problem:
You are using wrong operator “+”.
You are returning a wrong value from the function:
return easy(a+1,ans,n-1) + easy(a+1,ans+sum,n-1);
This will return the sum which would be a non-zero number.
Thus you code will always print yes.
Example:
1
4
1 2 3 5

Solution:
return either 1 or 0.
Modification:
return easy(a+1,ans,n-1) || easy(a+1,ans+sum,n-1);

Hope, this would help.

Made the changes still not working.

Hello @Rishabh3699,

Yes, it still has many issues in the logic you are applying.

  1. As you are adding “a[0]” (i.e. the last element of the array) to “ans” in the base condition.
    So, the sum will always be non-zero unless last element is not 0 itself.

  2. Even if you would correct the above logic, it won’t pass the test cases.
    Reason:
    You are trying to either ignore or include the elements in ans.
    So, for the case when all the elements are ignored, the ans will be zero and the code will always return yes.

Solution:
Check for the sum of each subset.

Modified code:

Hope, this would help.
Give alike if you are satisfied.

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.