Finding all subsets using backtracking c++ [closed]












-1














I am solving the problem of finding all subsets of a given set using backtracking. The following c++ code gives wrong result and I am not able to figure out why.
My logic is to remove each element from the given vector and call the function recursively on the remaining vector. A set is used to ensure only unique entities are stored.



void find(set<vector<int> > &s, vector<int> a){
for(int i=0;i<a.size();i++){
a.erase(a.begin()+i);
s.insert(a);
find(s,a);
}
}

vector<vector<int> > Solution::subsets(vector<int> &A) {
set<vector<int> > s;
find(s, A);
s.insert(A);
set<vector<int> >::iterator it;
vector<vector<int> > ans;
for(it=s.begin();it!=s.end();it++){
ans.push_back(*it);
}
return ans;
}









share|improve this question







New contributor




pooja is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











closed as off-topic by janos, Heslacher, Graipher, 200_success, Sᴀᴍ Onᴇᴌᴀ Dec 18 at 17:28


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Code not implemented or not working as intended: Code Review is a community where programmers peer-review your working code to address issues such as security, maintainability, performance, and scalability. We require that the code be working correctly, to the best of the author's knowledge, before proceeding with a review." – janos, Heslacher, Graipher, 200_success, Sᴀᴍ Onᴇᴌᴀ

If this question can be reworded to fit the rules in the help center, please edit the question.









  • 5




    Im sorry we don't help with wrong reaults. Once you have it working you can come back and we can help you make it better. That's how reviews work here.
    – bruglesco
    Dec 18 at 13:46
















-1














I am solving the problem of finding all subsets of a given set using backtracking. The following c++ code gives wrong result and I am not able to figure out why.
My logic is to remove each element from the given vector and call the function recursively on the remaining vector. A set is used to ensure only unique entities are stored.



void find(set<vector<int> > &s, vector<int> a){
for(int i=0;i<a.size();i++){
a.erase(a.begin()+i);
s.insert(a);
find(s,a);
}
}

vector<vector<int> > Solution::subsets(vector<int> &A) {
set<vector<int> > s;
find(s, A);
s.insert(A);
set<vector<int> >::iterator it;
vector<vector<int> > ans;
for(it=s.begin();it!=s.end();it++){
ans.push_back(*it);
}
return ans;
}









share|improve this question







New contributor




pooja is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











closed as off-topic by janos, Heslacher, Graipher, 200_success, Sᴀᴍ Onᴇᴌᴀ Dec 18 at 17:28


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Code not implemented or not working as intended: Code Review is a community where programmers peer-review your working code to address issues such as security, maintainability, performance, and scalability. We require that the code be working correctly, to the best of the author's knowledge, before proceeding with a review." – janos, Heslacher, Graipher, 200_success, Sᴀᴍ Onᴇᴌᴀ

If this question can be reworded to fit the rules in the help center, please edit the question.









  • 5




    Im sorry we don't help with wrong reaults. Once you have it working you can come back and we can help you make it better. That's how reviews work here.
    – bruglesco
    Dec 18 at 13:46














-1












-1








-1







I am solving the problem of finding all subsets of a given set using backtracking. The following c++ code gives wrong result and I am not able to figure out why.
My logic is to remove each element from the given vector and call the function recursively on the remaining vector. A set is used to ensure only unique entities are stored.



void find(set<vector<int> > &s, vector<int> a){
for(int i=0;i<a.size();i++){
a.erase(a.begin()+i);
s.insert(a);
find(s,a);
}
}

vector<vector<int> > Solution::subsets(vector<int> &A) {
set<vector<int> > s;
find(s, A);
s.insert(A);
set<vector<int> >::iterator it;
vector<vector<int> > ans;
for(it=s.begin();it!=s.end();it++){
ans.push_back(*it);
}
return ans;
}









share|improve this question







New contributor




pooja is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











I am solving the problem of finding all subsets of a given set using backtracking. The following c++ code gives wrong result and I am not able to figure out why.
My logic is to remove each element from the given vector and call the function recursively on the remaining vector. A set is used to ensure only unique entities are stored.



void find(set<vector<int> > &s, vector<int> a){
for(int i=0;i<a.size();i++){
a.erase(a.begin()+i);
s.insert(a);
find(s,a);
}
}

vector<vector<int> > Solution::subsets(vector<int> &A) {
set<vector<int> > s;
find(s, A);
s.insert(A);
set<vector<int> >::iterator it;
vector<vector<int> > ans;
for(it=s.begin();it!=s.end();it++){
ans.push_back(*it);
}
return ans;
}






backtracking






share|improve this question







New contributor




pooja is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question







New contributor




pooja is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question






New contributor




pooja is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked Dec 18 at 13:42









pooja

1




1




New contributor




pooja is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





pooja is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






pooja is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




closed as off-topic by janos, Heslacher, Graipher, 200_success, Sᴀᴍ Onᴇᴌᴀ Dec 18 at 17:28


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Code not implemented or not working as intended: Code Review is a community where programmers peer-review your working code to address issues such as security, maintainability, performance, and scalability. We require that the code be working correctly, to the best of the author's knowledge, before proceeding with a review." – janos, Heslacher, Graipher, 200_success, Sᴀᴍ Onᴇᴌᴀ

If this question can be reworded to fit the rules in the help center, please edit the question.




closed as off-topic by janos, Heslacher, Graipher, 200_success, Sᴀᴍ Onᴇᴌᴀ Dec 18 at 17:28


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Code not implemented or not working as intended: Code Review is a community where programmers peer-review your working code to address issues such as security, maintainability, performance, and scalability. We require that the code be working correctly, to the best of the author's knowledge, before proceeding with a review." – janos, Heslacher, Graipher, 200_success, Sᴀᴍ Onᴇᴌᴀ

If this question can be reworded to fit the rules in the help center, please edit the question.








  • 5




    Im sorry we don't help with wrong reaults. Once you have it working you can come back and we can help you make it better. That's how reviews work here.
    – bruglesco
    Dec 18 at 13:46














  • 5




    Im sorry we don't help with wrong reaults. Once you have it working you can come back and we can help you make it better. That's how reviews work here.
    – bruglesco
    Dec 18 at 13:46








5




5




Im sorry we don't help with wrong reaults. Once you have it working you can come back and we can help you make it better. That's how reviews work here.
– bruglesco
Dec 18 at 13:46




Im sorry we don't help with wrong reaults. Once you have it working you can come back and we can help you make it better. That's how reviews work here.
– bruglesco
Dec 18 at 13:46















active

oldest

votes






















active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes

Popular posts from this blog

Сан-Квентин

8-я гвардейская общевойсковая армия

Алькесар