Python solution with detailed explanation
Solution\n\n4Sum https://leetcode.com/problems/4sum/\n\n Brute force solution will be O(N^4).\n Optimized solution will be O(N^3). We will first sort the input.
**Solution**\n\n**4Sum** https://leetcode.com/problems/4sum/\n\n* Brute force solution will be O(N^4).\n* Optimized solution will be O(N^3). We will first sort the input. Then we will use the two pointer technique.\n* We will use the same optimizations which we used in 3SUM problem to avoid duplicates. \n* Reference: https://discuss.leetcode.com/topic/75883/python-solution-with-detailed-explanation\n```\nclass Solution(object):\n    def fourSum(self, nums, target):\n        """\n        :type nums: List[int]\n        :type target: int\n        :rtype: List[List[int]]\n        """\n        nums.sort()\n        N, result = len(nums), []\n        for i in range(N):\n            if i > 0 and nums[i] == nums[i-1]:\n                continue\n            for j in range(i+1, N):\n                if j > i+1 and nums[j] == nums[j-1]:\n                    continue\n                x = target - nums[i] - nums[j]\n                s,e = j+1, N-1\n                while s < e:\n                    if nums[s]+nums[e] == x:\n                        result.append([nums[i], nums[j], nums[s], nums[e]])\n                        s = s+1\n                        while s < e and nums[s] == nums[s-1]:\n                            s = s+1\n                    elif nums[s]+nums[e] < x:\n                        s = s+1\n                    else:\n                        e = e-1\n        return result\n```
[]