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

typedef long long s64;

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

class Solution {
public:
    void print_vector(const vector<int>& v) {
        cout << "[";
        for (auto& val : v) {
            cout << val << ", ";
        }
        cout << "]" << endl;
    }

    unordered_set<vector<int>, VectorHash> n_sum(vector<int>& nums, int start_index, int count, s64 target) {
        unordered_set<vector<int>, VectorHash> res;
        for (int i = start_index; i < nums.size(); ++i) {
            if (count == 1) {
                if (nums[i] == target) res.insert({nums[i]});
            } else {
                unordered_set<vector<int>, VectorHash> induce = n_sum(nums, i+1, count-1, target-nums[i]);
                for (const auto& v : induce) {
                    auto cur_v = v;
                    cur_v.push_back(nums[i]);
                    sort(cur_v.begin(), cur_v.end());
                    res.insert(cur_v);
                }
            }
        }
        return res;
    }

    vector<vector<int>> fourSum(vector<int>& nums, int target) {
        vector<int> filtered_nums;
        int dupe_count = 0;
        int prev = INT_MIN;
        for (int i = 0; i < nums.size(); ++i) {
            if (nums[i] == prev) {
                ++dupe_count;
            } else {
                prev = nums[i];
                dupe_count = 0;
            }
            if (dupe_count < 4) {
                filtered_nums.push_back(nums[i]);
            }
        }
        print_vector(filtered_nums);

        auto set =  n_sum(filtered_nums, 0, 4, target);
        vector<vector<int>> res;
        for (const auto& v : set) {
            res.push_back(v);
        }
        return res;
    }
};
24616