Subset sum problem

Hello, I am solving the subset sum problem. The approach I used is to count the total number of subsets adding upto 0 and then if the count of these subsets is greater than 0 then print Yes otherwise print No. I am not getting the desired output, please have a look and tell me where am I making mistakes.

Problem: https://online.codingblocks.com/app/player/79595/content/88835/4760/code-challenge

My code: https://ide.codingblocks.com/s/231959

@Ibrahim
dry run your code for
1
4
1 2 -4 6

Yes Ma’am I did a dry run on the test case you gave and my approach is not correct. Could you please tell me if I can modify anything in my approach to make it work? or if you could tell me some other approach to solve the problem.

@Ibrahim
The SubsetSumEasy problem can be divided into two subproblems
…a) Include the element, recur for j = j+1,
…b) Exclude the element, recur for j = j,
If any of the above the above subproblems return true, then return true.

Recursive Case
SubsetSumEasy(arr, i, out,j) = SubsetSumEasy(arr, i+1, out, j+1) OR
SubsetSumEasy(arr, i+1,out,j)
Base Cases : if sum of all elements of out ==0 && j>0 return true;
you can refer t this approach

Thank you. I understand the approach now.

1 Like

@Ibrahim mark your doubt as resolved and rate as well :slight_smile:

1 Like