Please help me in this problem. I’ve understood the problem but still the coding part is logically incorrect. Test cases are failing.
Subset Sum Problem
@prashantverma.vn
Problems I could find with your code :
- You should either traverse from left to right or from right to left. Either is fine.
When you go n-1 in your recursive call , that indicates right to left traversal. In that case you do not need an index variable at all. And your base condition should be n==0. - When you include the current element into your subset sum , show it in your sum by incrementing it.
sum + arr[n-1] // When you include the current element.
Leave it as is for excluding. - The second recursive call is never even going to be made ever since you returned the result from first call only. Instead make the two calls and store their results in boolean variables. Take the OR of these variables and return that.
- In your base condition , check whether sum==0. Then return true or false.
- Do not forget that there will always be a NULL subset for all arrays that does not include any element at all. The sum of this will always be 0 and we are not to consider this so deploy a proper condiion for that.
1 Like