codelink : what’s wrong in my code , before putting vector into set I am getting all ouput but after putting it some of the output is invisilble , please find the bug in my code
SUM IT UP, OUTpUT problem
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.
here’s my code link : https://pastebin.com/RrJDiEFn
problem 1 why not my approach of storing vector into set and then giving output is not working a(I have commented since it was producing no ouput)
2. Even if it produces output it is not in desired form of ouput how to do that adjustment
Hello @Divya_321
Do Not print the vector “b” while inserting it into the set “A” because then what is the use of the set. Instead first insert all the sum vectors “b” into the set and print the contents of the set at the end.
Here is the modified code
thanks for solving problem but what I was trying to do is first sorting content of eligible vectors(whose elements sums to 0 or target sum) and then inserting them in set, if that vector is and checking if that sorted vector already exits in set, if not than I was printing the vector at current moment , instead of first storing all vectors than printing each vector one by one , is it wrong approach
Your code was
if(b.size() != 0 && sum == 0){
sort(b.begin(), b.end());
A.insert(b);
// if(A.find(b) != A.end())
for(int i = 0; i < b.size(); i++)
cout<<b[i]<<" ";
b.clear();
cout<<endl;
}
You first inserted the sorted vector (‘b’) to set ‘A’ and then you checked
if(A.find(b) != A.end())
After inserting ‘b’ the above statement will never be true.
If we neglect the above statement then you are nowhere checking if the vector ‘b’ already existed before.
Your approach is also correct but the implementation is wrong.
Let me know if you need any help.