Brute to Optimal || Beginner-Friendly Solution || Easy Video Explanation in Hindi
Ek Video me Saara Funda Clear (Easy Explanation in Hindi)\n\n## Click Here : www.youtube.com/watch?v=RblHmfT8JLI&list=PL4JcMBI4LmknXBJsifFkvdrOix7ukBsHC&index=8
## Ek Video me Saara Funda Clear (Easy Explanation in Hindi)\n\n## Click Here : www.youtube.com/watch?v=RblHmfT8JLI&list=PL4JcMBI4LmknXBJsifFkvdrOix7ukBsHC&index=8\n\n# Complexity\n- Time complexity: $$O(N^3)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: $$O(N)$$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# C++\n```\nclass Solution {\npublic:\n    vector<vector<int>> fourSum(vector<int>& nums, int target) {\n        int n = nums.size();\n\n        vector<vector<int>> ans;\n        sort(nums.begin(), nums.end());\n\n        set<vector<int>> uniqueSets;\n\n        for(int i = 0; i < n; i++){\n            for(int j = i + 1; j < n; j++){\n                int k = j + 1;\n                int l = n - 1;\n\n                while(k < l){\n                    long long sum = static_cast<long long>(nums[i]) + nums[j] + nums[k] + nums[l];\n\n                    if(sum == target){\n                        vector<int> quadruplet = {nums[i], nums[j], nums[k], nums[l]};\n                        uniqueSets.insert(quadruplet);\n                        k++;\n                        l--;\n                    }\n                    else if(sum < target) k++;\n                    else if(sum > target) l--;\n                }\n            }\n        }\n        \n        ans.assign(uniqueSets.begin(), uniqueSets.end());\n        return ans;\n    }\n};\n```\n# Java\n```\nclass Solution {\n    public List<List<Integer>> fourSum(int[] nums, int target) {\n        int n = nums.length;\n\n        List<List<Integer>> ans = new ArrayList<>();\n        Arrays.sort(nums);\n\n        Set<List<Integer>> set = new HashSet<>();\n\n        for(int i=0; i<n; i++){\n            for(int j=i+1; j<n; j++){\n                int k = j+1;\n                int l = n-1;\n\n                while(k<l){\n                        long sum = (long) nums[i]+nums[j]+nums[k]+nums[l];\n\n                        if(sum == target){\n                            List<Integer> al = new ArrayList<>();\n                            al.add(nums[i]);\n                            al.add(nums[j]);\n                            al.add(nums[k]);\n                            al.add(nums[l]);\n\n                            set.add(al);\n                            k++;\n                            l--;\n                        }\n\n                        else if(sum < target) k++;\n                        else if(sum > target) l--;\n                    \n                }\n            }\n        }\n        \n        ans.addAll(set);\n        return ans;\n    }\n}\n```\n# Python3\n```\nclass Solution:\n    def fourSum(self, nums: List[int], target: int) -> List[List[int]]:\n        n = len(nums)\n        nums.sort()\n\n        ans = []\n        unique_set = set()\n\n        for i in range(n):\n            for j in range(i + 1, n):\n                k = j + 1\n                l = n - 1\n\n                while k < l:\n                    total_sum = nums[i] + nums[j] + nums[k] + nums[l]\n\n                    if total_sum == target:\n                        temp = [nums[i], nums[j], nums[k], nums[l]]\n                        unique_set.add(tuple(temp))\n                        k += 1\n                        l -= 1\n                    elif total_sum < target:\n                        k += 1\n                    elif total_sum > target:\n                        l -= 1\n\n        ans = list(unique_set)\n        return ans\n```\n## Please Upvote if you liked the Solution.
['Array', 'Hash Table', 'Two Pointers', 'Sorting', 'C++', 'Java', 'Python3']