Subset Problem Easy sum

#include
using namespace std;
int main()
{
int sum;
int t;
cin>>t;
while(t>0)
{
int n;
cin>>n;
int arr[n];
for(int i=0;i<n;i++)
{
cin>>arr[i];
}
for(int i=0;i<n;i++)
{
for(int j=i;j<n;j++)
{
sum=0;
for(int k=i;k<=j;k++)
{
sum=sum+arr[k];
}
if(sum==0)
{
cout<<“Yes”<<endl;
break;

            }
            else if(i==n-1)
            {
                cout<<"No";
            }
            
        }
    }
    
    t--;
}
return 0;

} There is a minute mistake in the code because NO is printing i dont know pls tell me…

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.

bool comparator(string a, string b) { if (a.find(b)==0 || b.find(a)==0) return a.length() > b.length(); return a < b; }

can you please tell me how this comparator is working.

This comparator first checks whether either of the strings is a prefix substring of the other one.
For example :
“abc” is a prefix substring of “abcdef” or “abcd” or any other string starting with “abc”.
If any of the two strings is a prefix substring of the other , the longer string comes first .

Otherwise , if neither of the strings are a prefix substring of the other then the comparator simply compares them normally … comparing ASCII values character by character.

Refer to this for a simple example - https://ide.codingblocks.com/s/91032

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.