Subset sum easy

i am not able to write the code with the logic in the video . Please help

Hi @mikkyimran
Approach to this question :
At each step during the recursion , we make a call once where we include the current element into our sum and another where we exclude. If at the end when we reach the end of the array , we have obtained the sum=0, we return true , else false. During the intermediate recursive calls , we check the result from both the recursive calls , one including the current element and one excluding and return their OR since even if one of them had the subset sum 0 then we have to return true.
Note that the NULL subset ( subset with no elements ) is not to be considered as that would always have sum 0. To avoid considering that , we take a flag variable bool included = false and mark it as true even if we include a single element. Else it remains false. The NULL subset would be the only subset where the flag would remain false. For all other subsets , they would have included at least one element and hence their flag would be true.

Give a try to this approach and if still you are unable to code for it then here is the code for this problem, have a look at it and try to understand :

And if your doubt is resolved then mark it resolved.

2 Likes