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


struct VectorHash {
    std::size_t operator()(const std::vector<int>& v) const {
        std::size_t hash = 0;
        for (int num : v) {
            hash ^= std::hash<int>()(num) + 0x9e3779b9 + (hash << 6) + (hash >> 2);
        }
        return hash;
    }
};

class Solution {
public:
    vector<vector<int>> twoSumPoint(vector<int>& sort_nums, long target, int start) {
        int end = size(sort_nums) - 1;
        vector<vector<int>> sol;

        while (start < end) {
            int cur_sum = (long) sort_nums[start] + (long) sort_nums[end];
            if (cur_sum == target) {
                sol.push_back({sort_nums[start], sort_nums[end]});
                start += 1;
                end -= 1;
            } else if (cur_sum > target) {
                end -= 1;
            } else {
                start += 1;
            }
        }
        return sol;
    }
    
    vector<vector<int>> fourSum(vector<int>& nums, int target) {
        unordered_set<vector<int>, VectorHash> sols;
        sort(nums.begin(), nums.end());
        
        for (int a = 0 ; a < size(nums) ; a++) {
            for (int b = a + 1 ; b < size(nums); b++) {
                long twoTarget= (long) target - (long) nums[a] - (long) nums[b];
                vector<vector<int>> two_sol = twoSumPoint(nums,
                                                        twoTarget,
                                                        b + 1);
                for (vector<int> two : two_sol) {
                    vector<int> cur_sol = {nums[a], nums[b], two[0], two[1]};
                    sort(cur_sol.begin(), cur_sol.end());
                    sols.insert(cur_sol);
                }
            }
        }
        vector<vector<int>> vec_sol(sols.begin(), sols.end());
        return vec_sol;
    }
};
200941