Skip to content

Commit f129f93

Browse files
authored
Subset Sum Easy Solution added
1 parent b3a6182 commit f129f93

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed

Hacker Blocks/Subset Sum Easy.cpp

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/* Hacker Blocks */
2+
/* Title - Subset Sum Easy */
3+
/* Created By - Akash Modak */
4+
/* Date - 14/07/2020 */
5+
6+
// Mike is a very passionate about sets. Lately, he is busy solving one of the problems on sets. He has to find whether if the sum of any of the non-empty subsets of the set A is zero.
7+
8+
// Input Format
9+
// The first line contains an integer T, which is the total number of test cases.
10+
// T test cases follow.
11+
// Each test case consists of two lines.
12+
// The first line consists of a single integer N, which is the number of elements present in the set A.
13+
// The second line contains N space separated integers denoting the elements of the set A.
14+
15+
// Constraints
16+
// 1 ≤ T ≤10
17+
// 1 ≤ N ≤ 4
18+
// -10^5 ≤ A[i] ≤ 10^5
19+
20+
// Output Format
21+
// Print the answer for each testcase in a new line.
22+
// If the sum of any of the subset is zero, then print “Yes” (without quotes) else print “No”(without quotes).
23+
24+
// Sample Input
25+
// 1
26+
// 4
27+
// 1 3 2 -3
28+
// Sample Output
29+
// Yes
30+
// Explanation
31+
// The sum of the subset {3,-3} is zero.
32+
33+
#include<iostream>
34+
using namespace std;
35+
int flag;
36+
void subsetSum(int a[],int n,int i,int sum){
37+
if(i==n){
38+
if(sum==0){
39+
flag++;
40+
}
41+
return;
42+
}
43+
44+
subsetSum(a,n,i+1,sum+a[i]);
45+
subsetSum(a,n,i+1,sum);
46+
}
47+
int main() {
48+
int n,t;
49+
cin>>t;
50+
while(t--){
51+
cin>>n;
52+
int a[n];
53+
for(int i=0;i<n;i++){
54+
cin>>a[i];
55+
}
56+
flag = 0;
57+
subsetSum(a,n,0,0);
58+
if(flag>1)
59+
cout<<"Yes"<<endl;
60+
else
61+
cout<<"No"<<endl;
62+
}
63+
return 0;
64+
}

0 commit comments

Comments
 (0)