C++ Simple and Clean O(n^2) Solution, Including detailed explanation
Steps:\n1. First we store in a map twoSums all the pairs of numbers, while the key is their sum and the value is a pair of indices.\n2. For each twoSum, we chec
**Steps:**\n1. First we store in a map `twoSums` all the pairs of numbers, while the key is their sum and the value is a pair of indices.\n2. For each twoSum, we check if we have its complementary to target in the map too.\n3. If so, we take the lists of indices of the key and the target-key, and pass them to the function `makePairs`.\n4. In `makePairs` we loop through the pairs of indices of both lists and check if the indices are not the same. That\'s because we don\'t want to use the same index twice.\n5. We sort the four results to avoid duplicate fourSums in different orders.\n6. Insert into a set to avoid duplicate fourSums.\n7. Last step is to convert the set into a vector and here we got our result :)\n```\nclass Solution {\npublic:\n    vector<vector<int>> fourSum(vector<int>& nums, int target) {\n        nums1 = nums;\n        for (int i = 0; i < nums.size(); i++) {\n            for (int j = i+1; j < nums.size(); j++) {\n                twoSums[nums[i] + nums[j]].push_back({i, j});\n            }\n        }\n        \n        for (auto [key, value] : twoSums) {\n            int tmp = target - key;\n            if (twoSums.find(tmp) != twoSums.end()) makePairs(twoSums[key], twoSums[tmp]);\n        }\n        \n        vector<vector<int>> res(fourSums.begin(), fourSums.end()); \n        return res;\n    }\n    \n    void makePairs(vector<pair<int, int>> arr1, vector<pair<int, int>> arr2) {\n        for (auto [a, b] : arr1) {\n            for (auto [c, d] : arr2) {\n                if (a != c && b != c && a != d && b != d) {\n                    vector<int> tmp = {nums1[a], nums1[b], nums1[c], nums1[d]};\n                    sort(tmp.begin(), tmp.end());\n                    fourSums.insert(tmp);\n                } \n            }\n        }\n    }\n\nprivate:\n    set<vector<int>> fourSums;\n    unordered_map<int, vector<pair<int, int>>> twoSums;\n    vector<int> nums1;\n};\n```\n**Like it? please upvote!**
['C']