4 Sum - Easy C++ Solution 100% Beat Solution
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\nT.C = $$O(n^3)$$\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\nS.C = $$O(no. of quards)$$ this is for returning ans but actually $$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        vector<vector<int>>ans;\n        sort(nums.begin(),nums.end());\n\n        for(int i=0; i<n; i++){\n            if(i>0 && nums[i] == nums[i-1]) continue;\n            for(int j = i+1; j<n; j++){\n                if(j > i+1  && nums[j] == nums[j-1]) continue;\n                int k = j+1;\n                int l = n-1;\n                while(k < l){\n                    long long sum = nums[i];\n                    sum += 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                        ans.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                    }\n                    else if(sum < target){\n                        k++;\n                    }\n                    else{\n                        l--;\n                    }\n                }\n            }\n        }\n        return ans;\n    }\n};\n```\n![Screenshot 2024-03-29 010953.png](https://assets.leetcode.com/users/images/81ac92af-476a-48ea-ac7b-1515e3f58b87_1711971953.7556007.png)\n
['C++']