✅C++ || BEATS 96% || OPTIMIZED
PLEASE UPVOTE IF YOU FIND MY APPROACH HELPFUL, MEANS A LOT \uD83D\uDE0A\n\nIntuition: We will fix two-pointers and then find the remaining two elements using tw
**PLEASE UPVOTE IF YOU FIND MY APPROACH HELPFUL, MEANS A LOT \uD83D\uDE0A**\n\n**Intuition:** We will fix two-pointers and then find the remaining two elements using two pointer technique as the array will be sorted at first.\n\n**Approach:**\n* Sort the array, and then fix two pointers, so the remaining sum will be (target \u2013 (nums[i] + nums[j])). \n* Now we can fix two-pointers, one front, and another back, and easily use a two-pointer to find the remaining two numbers of the quad. \n* In order to avoid duplications, we jump the duplicates, because taking duplicates will result in repeating quads. \n* We can easily jump duplicates, by skipping the same elements by running a loop.\n\n```\n// Input => arr[] = [4,3,3,4,4,2,1,2,1,1], target = 9\n// Output => [[1,1,3,4],[1,2,2,4],[1,2,3,3]]\n\nclass Solution {\npublic:\n    vector<vector<int>> fourSum(vector<int>& nums, int target) {\n        vector<vector<int>> res;\n        \n        if(nums.empty())\n            return res;\n        \n        int n = nums.size();\n        \n        // Approach requires sorting and 2-pointer approach\n        \n        // Step1 -> sorting\n        sort(nums.begin(),nums.end());\n        \n        \n        // Step2 -> 2-pointer \n        for(int i=0; i<n; i++)\n        {\n            long long int target3 = target - nums[i];\n            \n            for(int j=i+1; j<n; j++)\n            {\n                long long int target2 = target3 - nums[j];\n                \n                int front = j+1;\n                int back = n-1;\n                \n                while(front<back)\n                {\n                    // remaining elements to be found for quad sum\n                    int two_sum = nums[front] + nums[back];\n                    \n                    if(two_sum < target2)\n                        front++;\n                    else if(two_sum > target2)\n                        back--;\n                    \n                    else\n                    {\n                        // if two_sum == target2\n                        vector<int> quad(4,0);\n                        // quad.push_back(nums[i]);\n                        // quad.push_back(nums[j]);\n                        // quad.push_back(nums[front]);\n                        // quad.push_back(nums[back]);\n                        quad[0] = nums[i];\n                        quad[1] = nums[j];\n                        quad[2] = nums[front];\n                        quad[3] = nums[back];\n                        \n                        \n                        res.push_back(quad);\n                        \n                        // Processing the duplicates of number 3\n                        while(front < back && nums[front] == quad[2]) \n                            front++;\n                        \n                        // Processing the duplicates of number 4\n                        while(front < back && nums[back] == quad[3]) \n                            back--;\n                    }\n                    \n                }\n                // Processing the duplicates of number 2\n                    while(j + 1 < n && nums[j + 1] == nums[j]) \n                        j++;\n            }\n            // Processing the duplicates of number 2\n                    while(i + 1 < n && nums[i + 1] == nums[i]) \n                        i++;\n        }\n        \n        return res;\n    }\n};\n```
['C', 'C++']