Hi Aryan
You are approaching this problem wrong. The problem is about subsets(or subsequences) , not subarrays.
Subarrays are parts of array that are continuous while subsets may or may not be continuous .
We are supposed to consider the subsets in this problem.
For example for an array a[ ] = {1 , 2 , 8 ,-3 } has a subset { 1 , 2 , -3 } which gives sum as 0 and should hence print “Yes” . However your code would give “No” since it does not consider non-continuous subsets.
Use recursion to generate all possible subsets of the array and calculate the sum . Since it is mentioned that the size of array won’t exceed 4 , recursion wouldn’t take much time for such a small array.
If the sum of any one such subset is zero , print “yes” , else “No” .
Use a boolean flag variable to mark it so . That should help you in this code as well if you just want to consider the subarrays.
Initialise flag as false. Mark it true if a subset has sum 0. After the loop if flag== true , print “Yes” , else “No” . Its important that you check after the loop/recursive call and not in it otherwise it could give you some problems.