<p>Given an array <code>nums</code> of <code>n</code> integers, return <em>an array of all the <strong>unique</strong> quadruplets</em> <code>[nums[a], nums[b], nums[c], nums[d]]</code> such that:</p>

<ul>
	<li><code>0 &lt;= a, b, c, d&nbsp;&lt; n</code></li>
	<li><code>a</code>, <code>b</code>, <code>c</code>, and <code>d</code> are <strong>distinct</strong>.</li>
	<li><code>nums[a] + nums[b] + nums[c] + nums[d] == target</code></li>
</ul>

<p>You may return the answer in <strong>any order</strong>.</p>

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

<pre>
<strong>Input:</strong> nums = [1,0,-1,0,-2,2], target = 0
<strong>Output:</strong> [[-2,-1,1,2],[-2,0,0,2],[-1,0,0,1]]
</pre>

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

<pre>
<strong>Input:</strong> nums = [2,2,2,2,2], target = 8
<strong>Output:</strong> [[2,2,2,2]]
</pre>

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

<ul>
	<li><code>1 &lt;= nums.length &lt;= 200</code></li>
	<li><code>-10<sup>9</sup> &lt;= nums[i] &lt;= 10<sup>9</sup></code></li>
	<li><code>-10<sup>9</sup> &lt;= target &lt;= 10<sup>9</sup></code></li>
</ul>

class Solution {
private:
    string _sum(vector<int> nums) {
        long s = 0;
        for (int i : nums)
            s += i;
        if (s > INT_MAX || s < INT_MIN)
            return "fail";
        else
            return to_string(s);
    }

public:
    vector<vector<int>> fourSum(vector<int>& nums, int target) {
        int n = nums.size();
        vector<vector<int>> vv;
        if (n < 4) {
            return vv;
        }
        if (n == 4) {

            string res = _sum(nums);
            if(res == "fail")
                return vv;
            int sum = stoi(res);
            // cout << sum << endl;
            if (sum != -1 && sum == target) {
                vector<int> v(nums);
                vv.push_back(v);
                return vv;
            } else
                return vv;
        }

        sort(nums.begin(), nums.end());
        for (int jj : nums)
            cout << jj << " , ";
        cout << endl;

        for (int i = 0; i < nums.size() - 3; i++) {
            if (i > 0 && nums[i] == nums[i - 1]) {
                //i++;
                continue;
            }
            for (int j = i + 1; j < nums.size() - 2; j++) {
                if (j > i + 1 && nums[j] == nums[j - 1]) {
                   // j++;
                    continue;
                }
                int k = j + 1;
                int l = nums.size() - 1;

                while (k < l) {
                    // cout <<  nums[i] << "  " << nums[j] << " " << nums[k] <<
                    // " " <<  nums[l]  << endl;
                    if (k > j + 1 && nums[k] == nums[k - 1]) {
                        k++;
                        continue;
                    }
                    if (l < (nums.size() - 1) && nums[l] == nums[l + 1]) {
                        l--;
                        continue;
                    }
                    //int sum = nums[i] + nums[j] + nums[k] + nums[l];
                    vector<int> tmp{nums[i], nums[j], nums[k], nums[l]};
                    string res = _sum(tmp);

                    if(res == "fail") {
                        k++;
                        l--;
                        continue;
                    }                        
                    int sum = stoi(res);
                    // cout << " Sum : " << sum << "  :: " <<  nums[i] << "  "
                    // << nums[j] << " " << nums[k] << " " <<  nums[l]  << endl;

                    if (sum == target) {
                        //vector<int> tmp{nums[i], nums[j], nums[k], nums[l]};
                        vv.push_back(tmp);
                        k++;
                        l--;
                    } else if (sum < target) {
                        k++;
                    } else if (sum > target) {
                        l--;
                    }
                }
            }
        }
        return vv;
    }
};
465428