✅ EASIEST || 💯 BEATS || 🧑‍💻 JAVA || ⭐ TWO POINTERS
\n\n# \u2B50 Intuition\n Describe your first thoughts on how to solve this problem. \nThis problem builds on the "two-sum" and "three-sum" problems. To find fou
```\n```\n# \u2B50 Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n**This problem builds on the "two-sum" and "three-sum" problems. To find four numbers that add up to a target value, we can try a sorted array and use two nested loops for the first two numbers. For the remaining pair, we can apply a two-pointer approach to find all unique quadruplets.**\n\n```\n```\n# \u2B50 Approach\n<!-- Describe your approach to solving the problem. -->\n1. **Sort the Array**: Sorting the array helps simplify handling duplicate values and allows the two-pointer technique.\n\n2. **Nested Loops**: Use two nested loops to pick the first two numbers, nums[i] and nums[j]. Ensure these loops skip duplicates to avoid redundant calculations.\n\n3. **Two-Pointer Search**: For the remaining two numbers, use two pointers (k starting right after j and l at the end of the array).\n    - If the sum of nums[i], nums[j], nums[k], and nums[l] matches the target, add the quadruplet to the result.\n    - Adjust pointers k and l to find other potential pairs, ensuring you skip over duplicates.\n\n4. **Result Storage**: Store each unique quadruplet in a list and return this list of lists.\n\n```\n```\n# \u2B50 Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n**$$O(n3)$$ due to the nested loop structure and the two-pointer search for each unique pair.**\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n**$$O(m)$$, where $$m$$ is the number of unique quadruplets stored in the result list.**\n```\n```\n# \u2B50 Code\n```java []\nclass Solution {\n    public List<List<Integer>> fourSum(int[] nums, int target) {\n        List<List<Integer>> ans = new ArrayList<>();\n        int len = nums.length;\n\n        Arrays.sort(nums);\n\n        for (int i = 0; i < len - 3; i++) {\n            if (i > 0 && nums[i - 1] == nums[i]) \n                continue;\n\n            for (int j = i + 1; j < len - 2; j++) {\n                if (j > i + 1 && nums[j - 1] == nums[j])\n                    continue;\n                \n                int k = j + 1;\n                int l = len - 1;\n\n                while (k < l) {\n                    long sum = nums[i] + nums[j];\n                    sum += nums[k] + nums[l];\n\n                    if (sum == target) {\n                        ans.add(Arrays.asList(nums[i], nums[j], nums[k], nums[l]));\n                        k++;\n                        l--;\n\n                        while (k < l && nums[k - 1] == nums[k]) k++;\n                        \n                        while (k < l && nums[l + 1] == nums[l]) l--;\n                    } else if (sum < target)\n                        k++;\n                    else \n                        l--;\n                }\n            }\n        }\n\n        return ans;\n    }\n}\n```\n\n```\n```\n\n![upvote.png](https://assets.leetcode.com/users/images/78a73912-d1ac-4b9c-baa5-d95d17c9161d_1729847953.8020196.png)\n
['Array', 'Two Pointers', 'Sorting', 'Java']