Given an array nums of n integers, return an array of all the unique quadruplets [nums[a], nums[b], nums[c], nums[d]] such that:


	0 &lt;= a, b, c, d&nbsp;&lt; n
	a, b, c, and d are distinct.
	nums[a] + nums[b] + nums[c] + nums[d] == target


You may return the answer in any order.

&nbsp;
Example 1:

Input: nums = [1,0,-1,0,-2,2], target = 0
Output: [[-2,-1,1,2],[-2,0,0,2],[-1,0,0,1]]


Example 2:

Input: nums = [2,2,2,2,2], target = 8
Output: [[2,2,2,2]]


&nbsp;
Constraints:


	1 &lt;= nums.length &lt;= 200
	-109 &lt;= nums[i] &lt;= 109
	-109 &lt;= target &lt;= 109


class Solution:
    def fourSum(self, nums: List[int], target: int) -> List[List[int]]:
        
        if len(nums) < 4: return []
        
        nums.sort()
        res = []
        
        for i in range(len(nums)):
            for j in range(i + 1, len(nums)):
                l = j+1
                r = len(nums)-1
                while l < r:

                    sum_ = nums[i]+nums[j]+nums[l]+nums[r]
                    a = [nums[i], nums[j], nums[l], nums[r]]
                
                    if sum_ == target and a not in res:
                        res.append(a)
                  
                    if sum_ > target:
                        r -= 1
                    
                    else:
                        l += 1
                        while l < r and nums[l-1] == nums[l]:
                            l += 1
                  
        return res
        
        # An Upvote will be encouraging
                    
        
class Solution {
    public List<List<Integer>> fourSum(int[] nums, int target) {
        Arrays.sort(nums);
        List<List<Integer>> llans = new LinkedList<>();
        if(nums == null || nums.length <= 2){
            return llans;
        }
        for(int i=0;i<nums.length-3;i++){
            for(int j=i+1;j<nums.length-2;j++){
                int l = j+1;
                int r = nums.length-1;
                long ressum = target - nums[i];
                ressum = ressum - nums[j];
                while(l<r){
                    if(nums[l]+nums[r] == ressum){
                        List<Integer> ll = new LinkedList<>();
                        ll.add(nums[i]);
                        ll.add(nums[j]);
                        ll.add(nums[l]);
                        ll.add(nums[r]);
                        llans.add(ll);

                        while( l<r && nums[l]==nums[l+1]){l++;}
                        while( l<r && nums[r]==nums[r-1]){r--;}
                        l++;
                        r--;
                    }
                    else if(nums[l]+nums[r] < ressum){
                        l++;
                    }
                    else{
                        r--;
                    }
                }
                while( j<nums.length-1 && nums[j]==nums[j+1]){
                    j++;}

            }
            while( i<nums.length-1 && nums[i]==nums[i+1]){
                i++;}

        }
        return llans;
    }
}
class Solution {
public:
    vector<vector<int>> fourSum(vector<int>& nums, int target) {

        int n = nums.size();

        vector<vector<int> > answer;

        if(n<4) return answer;

        sort(nums.begin(),nums.end());

        for(int i=0;i<n;){
            for(int j=i+1;j<n;){

                int left = j+1;
                int right = n-1;

                long long int x = (long long int)target - (long long int)nums[i] - (long long int)nums[j];

                while(left<right){

                    if(x == nums[left]+nums[right]){
                    vector<int> vect{nums[i] , nums[j] , nums[left] , nums[right]};
                    answer.push_back(vect);

                    //skipping duplicates while moving right
                    int k = 1;
                    while((left+k)<n && nums[left+k]==nums[left]) ++k;

                    if((left+k)>=n) break;
                    else left = left+k;

                    //skipping duplicates while moving left
                    k = 1;
                    while((right-k)>=0 && nums[right-k]==nums[right]) ++k;

                    if((right-k)<0) break;
                    else right = right-k;

                }
                else{

                    if(x>nums[left]+nums[right]){

                        //skipping duplicates while moving right
                        int k = 1;
                        while((left+k)<n && nums[left+k]==nums[left]) ++k;

                        if((left+k)>=n) break;
                        else left = left+k;

                    }
                    else{

                        //skipping duplicates while moving left
                        int k = 1;
                        while((right-k)>=0 && nums[right-k]==nums[right]) ++k;

                        if((right-k)<0) break;
                        else right = right-k;

                    }

                }

                }

               //skipping duplicates while moving right
               int k = 1;
                while((j+k)<n && nums[j+k]==nums[j]) ++k;

                if((j+k)>=n) break;
                else j = j+k;

            }

            //skipping duplicates while moving right
            int k = 1;
            while((i+k)<n && nums[i+k]==nums[i]) ++k;

            if((i+k)>=n) break;
            else i = i+k;
        }

        return answer;
    }
};
var fourSum = function(nums, target) {
    nums.sort((a, b) => a - b);
    const res = [];
    
    for(let i=0; i<nums.length-3; i++) {
        for(let j=i+1; j<nums.length-2; j++) {
            let min = j + 1;
            let max = nums.length - 1;
            while(min < max) {
                const sum = nums[i] + nums[j] + nums[min] + nums[max];
                if(sum === target) {
                    res.push([nums[min], nums[max], nums[i], nums[j]]);
                    while(nums[min] === nums[min+1]) min++;
                    while(nums[max] === nums[max-1]) max--;
                    min++;
                    max--;
                }
                
                else if(sum > target) 
                    max--;
                else
                    min++
            }
            while(nums[j] === nums[j+1]) j++;
        }
        while(nums[i] === nums[i+1]) i++;
    }
    
    return res;
};
4Sum