Please help me with the logic to solve this question. The approach I thought would give duplicates too. So please help.
Sum it Up: Unable to get the logic
@ayushjain.aj300
You could sort the array first.
And then use a modified two pointer approach where you iterate from the beginning of the pointer window to the end of the pointer window.
Or you could simply use whatever method that you have in mind and we can easily handle the duplication part later.
There are several ways to approach this problem.
As you asked, I have applied my method. Now please help me in removing the duplicity of the combinations. Here is the code: https://ide.geeksforgeeks.org/x4sIZIBhfz
Instead of printing the output array , store your output in a vector of vectors .
Make a vector of vectors in global scope
vector < vector < int > > v ;
Where you printing the output array , do not do that and just push the entire array data as a vector into v .
After your recursive calls are done and you get back to the main( ) , you have yourself a vector of vectors v present having all the answers , including the repetions.
Call sort( ) on this vector.
sort(v.begin() , v.end() ) ;
Now start printing the data in v one by one. Make sure to check if the current vector being printed is not same as the previous one to avoid repetitions.
You can directly compare vectors using == .