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

template<typename T>
T xorshift(const T& n,int i){
  return n^(n>>i);
}

int32_t hash(const int32_t& v) {
  int32_t p = 0x55555555ul; // pattern of alternating 0 and 1
  int32_t c = 3423571495ul; // random uneven integer constant; 
  return c*xorshift(p*xorshift(v,16),16);
}

// if c++20 rotl is not available:
template <typename T,typename S>
typename std::enable_if<std::is_unsigned<T>::value,T>::type
constexpr rotl(const T n, const S i){
  const T m = (std::numeric_limits<T>::digits-1);
  const T c = i&m;
  return (n<<c)|(n>>((T(0)-c)&m)); // this is usually recognized by the compiler to mean rotation, also c++20 now gives us rotl directly
}

class int32_vector_hasher {
public:
  std::size_t operator()(std::vector<int32_t> const& vec) const {
    std::size_t ret = 0;
    for(auto& i : vec) {
      ret += i; //rotl(ret,11)^ std::hash<int>{}(i);
    }
    return ret;
  }
};

class Solution {
public:
    unordered_set<vector<int>, int32_vector_hasher> getSingleAnswer(vector<int>& nums, vector<vector<int>>& list1, vector<vector<int>>& list2){
        unordered_set<vector<int>, int32_vector_hasher> ans;

        for(vector<int> v1 : list1){
            for(vector<int> v2 : list2){
                if(v1[0] == v2[0] || v1[0] == v2[1] || v1[1] == v2[0] || v1[1] == v2[1]){
                    continue;
                }
                else{
                    vector<int> single_ans = {nums[v1[0]], nums[v1[1]], nums[v2[0]], nums[v2[1]]};
                    sort(single_ans.begin(), single_ans.end());
                    ans.insert(single_ans);
                }
            }
        }

        return ans;

    }
    void printVector(vector<int>& vec){
        for(int i=0; i<vec.size(); i++){
            cout<<vec[i]<<" ";
        }
        cout<<"\n";
    }

    vector<vector<int>> fourSum(vector<int>& nums, int target) {
        unordered_set<vector<int>, int32_vector_hasher> unique_four_tuple;
        unordered_map<int, vector<vector<int>>> two_sum;
        sort(nums.begin(), nums.end());

        vector<int> new_nums;
        new_nums.push_back(nums[0]);

        int repeats = 1;
        for(int i=1; i < nums.size(); i++){
            if(nums[i] == nums[i-1]){
                repeats += 1;
            }
            else{
                repeats = 1;
            }
            if(repeats <= 4){
                new_nums.push_back(nums[i]);
            }
        }

        printVector(new_nums);


        for(int i = 0; i < new_nums.size(); i++){
            for(int j = i+1; j < new_nums.size(); j++){
                int sum = new_nums[i] + new_nums[j];
                if(!two_sum.contains(sum)){
                    two_sum[sum] = {};
                }
                two_sum[sum].push_back({i, j});
            }
        }
        unordered_set<int> sums_seen;

        for(auto it = two_sum.begin(); it != two_sum.end(); it++){
            int sum = it->first;
            if(sums_seen.contains(sum)){
                continue;
            }
            long int req_sum_check = (long int)target - (long int)sum;
            if(req_sum_check < numeric_limits<int>::min() ||
            req_sum_check > numeric_limits<int>::max()){
                continue;
            }
            int req_sum = target - sum;
            
            sums_seen.insert(sum);
            sums_seen.insert(req_sum);

            auto it2 = two_sum.find(req_sum);

            if(it2 != two_sum.end())
            {
                unordered_set<vector<int>, int32_vector_hasher> ans_cand = getSingleAnswer(new_nums, it->second, it2->second);
                unique_four_tuple.insert(ans_cand.begin(), ans_cand.end());
            }
        }

        vector<vector<int>> ans(unique_four_tuple.begin(), unique_four_tuple.end());

        return ans;
    }
};
71636