Best O(N^3) Solution
Approach\nUsing Two Pointer\n\n# Complexity\n- Time complexity:\nO(n^3)\n\n- Space complexity:\nO(1)\n\n# Code\n\nclass Solution {\npublic:\n    vector<vector<i
# Approach\nUsing Two Pointer\n\n# Complexity\n- Time complexity:\n$$O(n^3)$$\n\n- Space complexity:\n$$O(1)$$\n\n# Code\n```\nclass Solution {\npublic:\n    vector<vector<int>> fourSum(vector<int>& nums, int target) {\n        int n = nums.size();\n        sort(nums.begin(), nums.end());\n        vector<vector<int>> quadruplets;\n        for (int i = 0; i < n; i++) {\n            if (i != 0 && nums[i] == nums[i-1])\n                continue;\n            for (int j = i+1; j < n; j++) {\n                if (j != i+1 && nums[j] == nums[j-1])\n                    continue;\n                int k = j+1, l = n-1; \n                while (k < l) {\n                    long long sum = nums[i] + nums[j];\n                    sum += nums[k];\n                    sum += nums[l];\n                    if (sum == target) {\n                        vector<int> temp = {nums[i], nums[j], nums[k], nums[l]};\n                        quadruplets.push_back(temp);\n                        k++;\n                        l--;\n                        while (k < l && nums[k] == nums[k-1]) k++;\n                        while (k < l && nums[l] == nums[l+1]) l--;\n                    } else if (sum > target) {\n                        l--;\n                    } else {\n                        k++;\n                    }\n                }   \n            }\n        }\n        return quadruplets;\n    }\n};\n```
['C++']