Optimized C++ solution, beats 99.8%
cpp\nvector<vector<int>> fourSum(vector<int>& nums, int target) {\n\tstd::vector<std::vector<int>> result;\n\tstd::size_t n = nums.size();\n\tif (n < 4) {\n\t\t
```cpp\nvector<vector<int>> fourSum(vector<int>& nums, int target) {\n\tstd::vector<std::vector<int>> result;\n\tstd::size_t n = nums.size();\n\tif (n < 4) {\n\t\treturn result;\n\t}\n\n\tstd::sort(nums.begin(), nums.end());\n\tfor (int i = 0; i < n-3; ++i) {\n\t\t/* Conditions for pruning */\n\t\t// target too small, no point in continuing\n\t\tif (target <= 0 and nums[i] > 0) break;\n\t\t// nums[i] has become too large, no point in continuing\n\t\tif (nums[i] + nums[i+1] + nums[i+2] + nums[i+3] > target) break;\n\t\t// nums[i] is so small, even the largest elements cannot help reach the sum\n\t\tif (nums[i] + nums[n-3] + nums[n-2] + nums[n-1] < target) continue; \n\t\t // skip duplicates\n\t\tif (i > 0 and nums[i] == nums[i-1]) continue;\n\n\t\t/* Now explore further */\n\t\tfor (int j = i+1; j < n - 2; ++j) {\n\t\t\t/* Some more pruning */\n\t\t\t// nums[j] has become too large, no point in continuing\n\t\t\tif (nums[i] + nums[j] + nums[j+1] + nums[j+2] > target) break;\n\t\t\t// nums[j] is so small, even the largest elements cannot help reach the sum\n\t\t\tif (nums[i] + nums[j] + nums[n-2] + nums[n-1] < target) continue; \n\t\t\tif (j > i+1 and nums[j] == nums[j-1]) continue; // skip duplicates\n\n\t\t\t/* Explore the solution space */\n\t\t\tint left = j+1, right = n-1;\n\t\t\twhile (left < right) {\n\t\t\t\tint sum = nums[i] + nums[j] + nums[left] + nums[right];\n\t\t\t\tif (sum == target) {\n\t\t\t\t\tresult.push_back({nums[i], nums[j], nums[left], nums[right]});\n\t\t\t\t\tint last_left = nums[left], last_right = nums[right];\n\t\t\t\t\twhile (left < right and nums[left] == last_left) ++left;\n\t\t\t\t\twhile (left < right and nums[right] == last_right) --right;\n\t\t\t\t} else if (sum < target) {\n\t\t\t\t\t++left;\n\t\t\t\t} else {\n\t\t\t\t\t--right;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\treturn result;\n}\n```
['Two Pointers', 'C', 'Sorting', 'C++']