Given a list arr of N integers, print sums of all subsets in it in C++
Login/Signup to comment
3 comments on “Given a list arr of N integers, print sums of all subsets in it in C++”
×
30+ Companies are Hiring
Get Hiring Updates right in your inbox from PrepInsta
def sub(aray,output,index,ans):
if index>=len(aray):
a=sum((output[:]))
ans.append(a)
print(ans)
return
else:
#exclude
sub(aray,output,index+1,ans)
#include
element=aray[index]
output.append(element)
sub(aray,output,index+1,ans)
output.pop()
aray=[1,2,3]
output=[]
ans=[]
index=0
print(sub(aray,output,index,ans))
Hey, join our Discord Channel for technical queries
#include
using namespace std;
int findSum(string num)
{
int sum=0;
for(int i=0;i<num.length();i++){
sum= sum+num[i]-48;
}
return sum;
}
void powerSet(string str, int i, string curr)
{
int n=str.length();
if(i==n)
{
int x = findSum(curr);
cout<<x<<endl;
return;
}
powerSet(str,i+1,curr+str[i]);
powerSet(str,i+1,curr);
}
int main()
{
string str;
string arr[]= {"4", "3", "5"};
int n = sizeof(arr)/sizeof(arr[0]);
for(int i=0;i<n;i++)
str=str+arr[i];
powerSet(str,0,"");
return 0;
}