I am having difficulty determining if the contents inside the vector are unique. For instance {1,3,2} and {2,3,1} are the same combinations. What approach do you use ? This is my code.
vector<vector<int>> threeSum(std::vector<int> vec,int sum)
{
vector<vector<int>> output;
//First sort the vectors
std::sort(vec.begin(),vec.end());
for(int i=0;i<vec.size();i++)
{
int j=i+1;
size_t k = vec.size()-1;
while(j<k)
{
int res = vec[i]+vec[j]+vec[k];
if(res==sum){
//Make sure we already dont have it - Make sure only uniques are taken
std::cout << "Found it";
output.push_back({vec[i], vec[j], vec[k]});
j++;
}
else if (res < sum ){
//result is small - increase it
j++;
}
else {
//result is large - lets decrease it
k--;
}
}
}
return output;
}Any suggestions would be appreciated ?