Finding all subsets using backtracking c++ [closed]
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
New contributor
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.
add a comment |
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
New contributor
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
add a comment |
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
New contributor
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
backtracking
New contributor
New contributor
New contributor
asked Dec 18 at 13:42
pooja
1
1
New contributor
New contributor
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
add a comment |
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
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
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