<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:

    vector<int> get_sorted(vector<int> nums, int start, int end)
    {

        vector<int> temp, first, second;
        temp.clear();
        if(start==end)
        {
            temp.push_back(nums[start]);
            return temp;
            
        }

        int mid = (start + end)/2;

        first = get_sorted(nums, start, mid);

        second = get_sorted(nums, mid+1, end);

        int i=0,j=0;
        temp.clear();

        while(i<first.size() || j< second.size())
        {
            if(i>=first.size() && j< second.size())
            {
                temp.push_back(second[j]);
                j++;
                continue;
            }

            if(j>=second.size() && i<first.size())
            {
                temp.push_back(first[i]);
                i++;
                continue;
            }

            if(first[i]<=second[j])
            {
                temp.push_back(first[i]);
                i++;
            }
            else
            {
                temp.push_back(second[j]);
                j++;
            }

        }

            return temp;


    }

    vector<vector<int>> threeSum(vector<int>& nums) {

        vector<int> sorted_num;
        vector< vector<int> > sol;

        sorted_num = get_sorted(nums, 0, nums.size()-1);

       /* for(int k=0;k<sorted_num.size();k++)
        {
            cout << sorted_num[k] << endl;
        }*/

        int start=0, end = sorted_num.size()-1, sum=0, temp=0, zc=0;

       for(int g=0;g<sorted_num.size();g++)
       {

            if(sorted_num[g]==0)
            {
                zc++;

            }
       }

        for(int i=0; i<sorted_num.size(); i++)
        {
            if(sorted_num[i]>=0 && zc<3)
            {
                continue;
            }

            if((i-1)>=0 && (sorted_num[i]==sorted_num[i-1]))
            {
                //cout << "skip " << i << " " << sorted_num[i] << endl; 
                continue;
            }
            //cout << " at " << i << sorted_num[i] << endl;
            sum = sorted_num[i]*(-1);
            start=i+1, end = sorted_num.size()-1;

                while(start<end)
                {
                    if(start==i)
                    {
                        start++;
                        continue;
                    }

                    if(end==i)
                    {
                        end--;
                        continue;

                    }
                    temp = sorted_num[start] + sorted_num[end];
                 //   cout << "temp " << temp << endl;

                    /*
                    if(temp==sum)
                    {
                        cout << "found m at " << sorted_num[start] << sorted_num[end] << endl;
                        int rf=start, rs=end;

                        int fc = 1, sc = 1;

                        while((rf<rs) && ((rf+1)<sorted_num.size()) && ((rs-1)>0))
                        {
                            if(sorted_num[rf]==sorted_num[rf+1])
                            {
                                fc = fc + 1;
                                rf++;
            
                            }

                            if(sorted_num[rs]==sorted_num[rs-1])
                            {
                                sc = sc + 1;
                                rs--;

                            }
                        }

                        int comb = fc*sc;
                        
                        for(int l=0; l<comb;l++)
                        {
                        vector<int> ch;
                        ch.push_back(i);
                        ch.push_back(sorted_num[start]);
                        ch.push_back(sorted_num[end]);

                        sol.push_back(ch);
                        }

                        start = rf;
                        end = rs;


                        
                    }*/


                    if(temp==sum)
                    {
                       // cout << "found m " << endl << temp;
                         vector<int> ch;
                        ch.push_back(sorted_num[i]);
                        ch.push_back(sorted_num[start]);
                        ch.push_back(sorted_num[end]);

                        sol.push_back(ch);

                        while( (start+1) < sorted_num.size())
                        {
                           // cout << "s " << start << endl; 
                            if(sorted_num[start+1]==sorted_num[start])
                            {
                                start++;
                            }
                            else
                            {
                                break;
                            }
                        }

                        start++;
                        

                        while((end-1)>0)
                        {
                            if(sorted_num[end-1]==sorted_num[end])
                            {
                                end--;
                            }
                            else
                            {
                                break;
                            }

                        }
                        end--;
                        
                        //start++;

                    }

                    if(temp>sum)
                    {
                        end--;
                    }

                    if(temp<sum)
                    {
                        start++;
                    }

                }

               

        }


        return sol;

        
    }
};
351398