Most optimal solution 🍃| Detailed explanation ☑️☑️
Approach\n1. First, we will sort the entire array.\n2. We will use a loop(say i) that will run from 0 to n-1. This i will represent one of the fixed pointers. I
# Approach\n1. First, we will sort the entire array.\n2. We will use a loop(say i) that will run from 0 to n-1. This i will represent one of the fixed pointers. In each iteration, this value will be fixed for all different values of the rest of the 3 pointers. Inside the loop, we will first check if the current and the previous element is the same and if it is we will do nothing and continue to the next value of i.\n3. After checking inside the loop, we will introduce another fixed pointer j(runs from i+1 to n-1) using another loop. Inside this second loop, we will again check for duplicate elements and only perform any further operation if the elements are different.\n4. Inside the second loop, there will be 2 moving pointers i.e. k(starts from j+1) and l(starts from the last index). The pointer k will move forward and the pointer l will move backward until they cross each other while the value of i and j will be fixed.\n5. Now we will check the sum i.e. nums[i]+nums[j]+nums[k]+nums[l].\n- If the sum is greater, then we need lesser elements and so we will decrease the value of l.\n- If the sum is lesser than the target, we need a bigger value and so we will increase the value of k. \n- If the sum is equal to the target, we will simply insert the quad i.e. nums[i], nums[j], nums[k], and nums[l] into our answer and move the pointers k and l skipping the duplicate elements(i.e. by checking the adjacent elements while moving the pointers).\n6. Finally, we will have a list of unique quadruplets.\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity: O(N^3)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(1)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# PS\n- If there\'s any optimal code then do lmk, thank you :)\n\n# Code\n```\nclass Solution {\npublic:\n    vector<vector<int>> fourSum(vector<int>& nums, int target) {\n        // Get the size of the input vector\n        int n = nums.size();\n        \n        // Sort the input vector to simplify the search process\n        sort(nums.begin(), nums.end());\n        \n        // Vector to store the resulting quadruplets\n        vector<vector<int>> temp;\n        \n        // Loop through each element in the sorted vector\n        for (int i = 0; i < n; i++) {\n            // Skip duplicates of the first element to avoid duplicate quadruplets\n            if (i > 0 && nums[i] == nums[i - 1])\n                continue;\n            \n            // Loop through each element after the first element\n            for (int j = i + 1; j < n; j++) {\n                // Skip duplicates of the second element to avoid duplicate quadruplets\n                if (j > i + 1 && nums[j] == nums[j - 1])\n                    continue;\n                \n                // Pointers for the third and fourth elements of the quadruplet\n                int k = j + 1;\n                int l = n - 1;\n                \n                // While the third pointer is less than the fourth pointer\n                while (k < l) {\n                    // Calculate the sum of the current quadruplet\n                    //long long sum = nums[i] + nums[j] + nums[k] + nums[l];\n                    long long sum = static_cast<long long>(nums[i]) + nums[j] + nums[k] + nums[l];\n\n                    \n                    // If the sum equals the target, add the quadruplet to the result vector\n                    if (sum == target) {\n                        vector<int> ans = {nums[i], nums[j], nums[k], nums[l]};\n                        temp.push_back(ans);\n                        \n                        // Move the third pointer to the right and the fourth pointer to the left\n                        k++;\n                        l--;\n                        \n                        // Skip duplicates of the third and fourth elements\n                        while (k < l && nums[k] == nums[k - 1])\n                            k++;\n                        while (k < l && nums[l] == nums[l + 1])\n                            l--;\n                    } \n                    // If the sum is less than the target, move the third pointer to the right\n                    else if (sum < target) {\n                        k++;\n                    } \n                    // If the sum is greater than the target, move the fourth pointer to the left\n                    else {\n                        l--;\n                    }\n                }\n            }\n        }\n        // Return the resulting vector of quadruplets\n        return temp;\n    }\n};\n\n```\n![upvote.jpg](https://assets.leetcode.com/users/images/5a439329-20ca-4f8b-9711-45d5becc808a_1709401668.2592373.jpeg)\n
['Array', 'Two Pointers', 'Sorting', 'C++']