<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 {
public:
map<pair<int, int>, int> kol;
  vector <int> c;
//   bool comp(pair<pair<int, int>, set<pair<int, int>>> x, pair<pair<int, int>, set<pair<int, int>>> y) {
//     return c[x.first.first] < c[y.first.first];
// }
vector<vector<int>> fourSum(vector<int>& nums, int target) {
    map<long long, vector< pair<pair<int, int>, set<pair<int, int>>>>> mp;
    c = nums;
    
    
    vector <vector<int>> ans;
    for (int i = 0; i < nums.size(); i++) {
        for (int j = i + 1; j < nums.size(); j++) {
            kol[{min(nums[i], nums[j]), max(nums[i], nums[j])}]++;
            mp[nums[i] + nums[j]].push_back({{i, j}, {}});
           
        }
    }
    set<pair<pair<int, int>, pair<int, int>>> st;
    vector <int> cnt(4);
   for (auto k : mp) {
       sort(mp[k.first].begin(), mp[k.first].end(), [&](pair<pair<int, int>, set<pair<int, int>>> x, pair<pair<int, int>, set<pair<int, int>>> y) {
    return c[x.first.first] < c[y.first.first];
});
   }
    
    pair<int, int> aa;
    pair<int, int> bb;
    
    for (auto k : mp) {
        vector< pair<pair<int, int>, set<pair<int, int>>>> a = k.second;
        vector< pair<pair<int, int>, set<pair<int, int>>>> b = mp[target - k.first];
        if (a.size() == 0 || b.size() == 0) {
            continue;
        }
        pair<int, int> prev = {1e9 + 1, 1e9 +1};
        
        
        for (auto i : a) {
            int l = 0;
            while (l < b.size()) {
                auto j = b[l];
                if (prev.first == nums[j.first.first] && prev.second == nums[j.first.second]) {
                    int k = kol[{min(nums[j.first.first], nums[j.first.second]), max(nums[j.first.first], nums[j.first.second])}];
                    l += k;
                    continue;
                }
                l++;
                if (i.first.first != j.first.first &&
                    i.first.first != j.first.second &&
                    i.first.second != j.first.first &&
                    i.first.second != j.first.second) {
                    
                    
                        cnt[0] = nums[i.first.first];
                        cnt[1] = nums[i.first.second];
                        cnt[2] = nums[j.first.first];
                        cnt[3] = nums[j.first.second];
                        sort(cnt.begin(), cnt.end());
                    aa.first = cnt[0];
                    aa.second = cnt[1];
                    bb.first = cnt[2];
                    bb.second = cnt[3];
                    if (st.find({aa, bb}) == st.end())
                        if (j.second.find(i.first) == j.second.end() &&
                            i.second.find(j.first) == i.second.end()) {
                            
                            j.second.insert(i.first);
                            i.second.insert(j.first);
                            
                            st.insert({aa, bb});
                            
                            
                            ans.push_back(cnt);
                            prev.first = nums[j.first.first];
                            prev.second = nums[j.first.second];
                        }
                }
            }
        }
    }
    
    return ans;
}
};
183308