<p>Given an integer array nums, return all the triplets <code>[nums[i], nums[j], nums[k]]</code> such that <code>i != j</code>, <code>i != k</code>, and <code>j != k</code>, and <code>nums[i] + nums[j] + nums[k] == 0</code>.</p>

<p>Notice that the solution set must not contain duplicate triplets.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> nums = [-1,0,1,2,-1,-4]
<strong>Output:</strong> [[-1,-1,2],[-1,0,1]]
<strong>Explanation:</strong> 
nums[0] + nums[1] + nums[2] = (-1) + 0 + 1 = 0.
nums[1] + nums[2] + nums[4] = 0 + 1 + (-1) = 0.
nums[0] + nums[3] + nums[4] = (-1) + 2 + (-1) = 0.
The distinct triplets are [-1,0,1] and [-1,-1,2].
Notice that the order of the output and the order of the triplets does not matter.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> nums = [0,1,1]
<strong>Output:</strong> []
<strong>Explanation:</strong> The only possible triplet does not sum up to 0.
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> nums = [0,0,0]
<strong>Output:</strong> [[0,0,0]]
<strong>Explanation:</strong> The only possible triplet sums up to 0.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>3 &lt;= nums.length &lt;= 3000</code></li>
	<li><code>-10<sup>5</sup> &lt;= nums[i] &lt;= 10<sup>5</sup></code></li>
</ul>

class Solution {
public:
    // int find_index(vector<int> v, int index1, int index2)
    // {
    //     int elem = -(v[index1] + v[index2]);
    //     int start = 0, end = int(v.size()) -1 ;
    //     while(start <= end)
    //     {
    //         int mid = (start + end)/2;
    //         if(v[mid] == elem)
    //         {
    //             if(mid == index1 || mid == index2)
    //             {
    //                 if(mid - 1 >= 0 && v[mid-1] == elem && mid-1 != index1 && mid-1 != index2)
    //                 {
    //                     return mid -1;
    //                 }
    //                 else if(mid + 1 <= int(v.size()) - 1 && v[mid + 1] == elem && mid+1 != index1 && mid+1 != index2)
    //                 { 
    //                     return mid + 1;
    //                 }
    //                 else
    //                 {
    //                     return -1;
    //                 }
    //             }
    //             return mid;
    //         }
    //         else if(elem > v[mid])
    //         {
    //             start = mid+ 1; 
    //         }
    //         else // elem < v[mid]
    //         {
    //             end = mid-1;
    //         }
    //     }
    //     return -1;
    // }
    // vector<vector<int>> check_2sum(vector<int> v, int index1)
    // {// returns all pairs with sum -(v[index1])
    //     vector<vector<int>> vec_2sum ;
    //     for(int index2 = 0; index2 < int(v.size()); index2++)
    //     {
    //         if(index2 != index1)
    //         {
    //             int index3 = find_index(v, index1, index2);
    //             if(index3 != -1)
    //             {
    //                 // triplet found
    //                 vector<int> v1 = {v[index1], v[index2], v[index3]};
    //                 sort(v1.begin(), v1.end());
    //                 vec_2sum.push_back(v1);
    //             }
    //         }
    //     }
    //     return vec_2sum;
    // }
    vector<vector<int>> find_2sum_pairs(vector<int> v, int index)
    {
        int sum = -v[index];
        int p1 = 0, p2 = int(v.size())-1;
        vector<vector<int>> ans;
        while(p1 < p2)
        {
            if(v[p1] + v[p2] == sum )
            {
                if(p1 != index && p2 != index)
                {
                vector<int> temp = {v[index], v[p1], v[p2]};
                sort(temp.begin(), temp.end());
                ans.push_back(temp);
                }
                p1++; p2--;
            }
            else if(v[p1] + v[p2] < sum)
            {
                ++p1; // we need to increase the pair sum
            }
            else // v[p1] + v[p2] > sum   ---  then we need to decrease the pair sum
            {
                --p2;
            }
        }
        return ans;
    }
    vector<vector<int>> threeSum(vector<int>& nums) {
        // cout<<"\n\n";
        // cout<<int(nums.size());
        // cout<<"\n\n";
        sort(nums.begin(), nums.end()); 
        set<vector<int>> set_triplets;
        vector<int> nums_temp;
        for(auto& elem: nums)
        {
            if(int(nums_temp.size()) == 0 || int(nums_temp.size()) == 1)
            {
                nums_temp.push_back(elem);
            }
            else if(nums_temp[int(nums_temp.size()) - 1] != elem)
            {
                nums_temp.push_back(elem);
            }
            else if(nums_temp[int(nums_temp.size()) - 1] == elem && nums_temp[int(nums_temp.size()) - 2] != elem)
            {
                nums_temp.push_back(elem);
            }
            else if( (int(nums_temp.size()) == 2 && nums_temp[int(nums_temp.size()) - 1] == 0 && nums_temp[int(nums_temp.size()) - 2] == 0 && elem == 0)
            ||       (int(nums_temp.size()) > 2 && nums_temp[int(nums_temp.size()) - 1] == 0 && nums_temp[int(nums_temp.size()) - 2] == 0 && nums_temp[int(nums_temp.size()) -3 != 0 ] && elem == 0) )
            {
                nums_temp.push_back(elem);
            }
        }
        for(int i = 0 ; i < int(nums_temp.size()); i++)
        {
            vector<vector<int>> temp_vec = find_2sum_pairs(nums_temp, i);
            for(auto& vec: temp_vec)
            {
                set_triplets.insert(vec);
            }
        }
        vector<vector<int>> ans(set_triplets.begin(), set_triplets.end());
        // vector<vector<int>> ans = {{1, 8, 9}};
        return ans;
        // return set_triplets;
    }
};
233528