4 Sum

PHOTO EMBED

Mon Aug 01 2022 13:01:59 GMT+0000 (Coordinated Universal Time)

Saved by @Ranjan_kumar #c++

class Solution {
public:
    vector<vector<int>> fourSum(vector<int>& nums, long long int target) {
        int n=nums.size();
        set<vector<int>>s;
        sort(nums.begin(),nums.end());
        for(int i=0;i<n-3;i++)
        {
            
           for(int j=i+1;j<n-2;j++)
           {
              
               int k=j+1;
               int l=n-1;
               while(k<l)
               {
                   long long int t=0LL+nums[i]+nums[j]+nums[k]+nums[l];
                   if(t==target)
                   {
                       vector<int>v;
                       v.push_back(nums[i]);
                       v.push_back(nums[j]);
                       v.push_back(nums[k]);
                       v.push_back(nums[l]);
                       s.insert(v);
                      
                       
                   }
                   if(t>target) l--;
                   else k++;
               }
           }
        }
        vector<vector<int>>v1;
        set<vector<int>>::iterator it;
        for(it=s.begin();it!=s.end();it++)
        {
            v1.push_back(*it);
        }
        return v1;
    }
};
content_copyCOPY

https://leetcode.com/problems/4sum/