Subset Sum Easy

whats wrong with my code?

You should use logical OR operator instead of bitwise or
do not start with o = -1
instead check for condition like
if(o+a[i] == 0) return true;

You have to do it using dynamic programming since the program would run in 2^n time so it would give TLE,
If u need help for building the solution do ask

@chhavibansal what should i use instead of logical OR?
and why its not correct?

hi sorry a small error from my end | is bitwise OR, u should be using || ie the logical or which is called the short circuit operator , in case u happen to find a subset where the sum comes out to be 0 the function trues at the point in time abandoning the processing any furthur.

So use logical OR ( || ) instead of ( | )
Using | is not correct since we are not working on comparing the bit value here, instead where are comparing true and false.
IT would not lead to ur short curcuit logic at any point

@chhavibansal ok …i understood
can you tell me how this boolean data type stored and compared?

and pls look this one …still not working https://ide.codingblocks.com/s/180735

So, I got the catch
If are considering 2 cases,
once we include the current element to the sum , otherwise we dont

so since we are sending 0 as the initial o value so the case when we are just traversing and not adding the current value to the o value we reach i == n here we were checking if o == 0 we returned true
but this would happen always irrespective of the case that we have a subset equal to sum 0 or not .

So i am taking a global variable count that is incremented each time a sum 0 is encountered.

if count == 2 we return true else false is returned

The code below would help u clear ur logic

@chhavibansal i got ur point but … i am returning true only when the sum is 0, otherwise return false
and i am returning (funtion1 OR function2)…it means if i encounter any one true at at the base case it will return true to the called function …ie (true||false)=true;

@chhavibansal moreover the code u provided still not passing all the test case :frowning_face::frowning_face:

hello!!! anyone help me out pls

THIS IS 100% working code added comments in the code itself, I do not know why it did not work well for recursion but i have used bitmasking for making all possible subsets of the given array and then i find the sum of each subset . if the subset sum == 0 then i return true

else we iterate further

Sorry for the late response

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.

1 Like