Best Solution for Arrays 🚀 in C++, Python and Java || 100% working
Intuition✨ The problem requires finding all unique quadruplets in the array that sum up to the target.
✨ Sorting the array and using pointers help reduce the pr
# Intuition  
✨ The problem requires finding all unique quadruplets in the array that sum up to the target.  
✨ Sorting the array and using pointers help reduce the problem's complexity.  

# Approach  
1️⃣ Sort the array to efficiently skip duplicates.  
2️⃣ Use two nested loops to fix the first two numbers.  
3️⃣ Use two pointers to find the remaining two numbers that complete the target sum.  
4️⃣ Skip duplicates to ensure unique quadruplets.  

# Complexity  
- Time complexity: $$O(n^3)$$  
- Space complexity: $$O(1)$$  

# Code  
```cpp []
#define ll long long
class Solution {
public:
    vector<vector<int>> fourSum(vector<int>& nums, int target) {
        int n = nums.size();
        sort(nums.begin(), nums.end());
        vector<vector<int>> ans;
        for (int i = 0; i < n; i++) {
            if (i > 0 && nums[i] == nums[i - 1]) continue;
            for (int j = i + 1; j < n; j++) {
                if (j > i + 1 && nums[j] == nums[j - 1]) continue;
                ll comp = (ll)target - (nums[i] + nums[j]);
                int k = j + 1, l = n - 1;
                while (k < l) {
                    if (nums[k] + nums[l] == comp) {
                        ans.push_back({nums[i], nums[j], nums[k], nums[l]});
                        while (k < l && nums[k] == nums[k + 1]) k++;
                        while (k < l && nums[l] == nums[l - 1]) l--;
                        k++;
                        l--;
                    } else if (nums[k] + nums[l] > comp) l--;
                    else k++;
                }
            }
        }
        return ans;
    }
};
```

```python []
Code
class Solution:
    def fourSum(self, nums: List[int], target: int) -> List[List[int]]:
        nums.sort()
        n = len(nums)
        ans = []
        for i in range(n):
            if i > 0 and nums[i] == nums[i - 1]:
                continue
            for j in range(i + 1, n):
                if j > i + 1 and nums[j] == nums[j - 1]:
                    continue
                comp = target - (nums[i] + nums[j])
                k, l = j + 1, n - 1
                while k < l:
                    if nums[k] + nums[l] == comp:
                        ans.append([nums[i], nums[j], nums[k], nums[l]])
                        while k < l and nums[k] == nums[k + 1]:
                            k += 1
                        while k < l and nums[l] == nums[l - 1]:
                            l -= 1
                        k += 1
                        l -= 1
                    elif nums[k] + nums[l] > comp:
                        l -= 1
                    else:
                        k += 1
        return ans
```

```java []
Code
class Solution {
    public List<List<Integer>> fourSum(int[] nums, int target) {
        Arrays.sort(nums);
        int n = nums.length;
        List<List<Integer>> ans = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            if (i > 0 && nums[i] == nums[i - 1]) continue;
            for (int j = i + 1; j < n; j++) {
                if (j > i + 1 && nums[j] == nums[j - 1]) continue;
                long comp = (long)target - (nums[i] + nums[j]);
                int k = j + 1, l = n - 1;
                while (k < l) {
                    if (nums[k] + nums[l] == comp) {
                        ans.add(Arrays.asList(nums[i], nums[j], nums[k], nums[l]));
                        while (k < l && nums[k] == nums[k + 1]) k++;
                        while (k < l && nums[l] == nums[l - 1]) l--;
                        k++;
                        l--;
                    } else if (nums[k] + nums[l] > comp) {
                        l--;
                    } else {
                        k++;
                    }
                }
            }
        }
        return ans;
    }
}
```
<img src="https://assets.leetcode.com/users/images/7b864aef-f8a2-4d0b-a376-37cdcc64e38c_1735298989.3319144.jpeg" alt="upvote" width="150px">

# Connect with me on LinkedIn for more insights! 🌟 Link in bio

['Array', 'Two Pointers', 'Sorting', 'Python', 'C++', 'Java', 'Python3']