Skip to content

Latest commit

 

History

History
31 lines (24 loc) · 843 Bytes

Day 108.md

File metadata and controls

31 lines (24 loc) · 843 Bytes

Day 108

Link: https://x.com/kom_senapati/status/1780936214327300361

Approach

  • Sorted the array and created ans set to store vectors
  • Used three loops to iterate over all triplets and find remaining target if it was in array then inserted the quadruplet to ans
  • Atlast returned ans

Code

vector<vector<int> > Solution::fourSum(vector<int> &A, int target) {
    sort(A.begin(),A.end());
    set<vector<int>>ans;

    for(int i=0;i<A.size();i++){
        for(int j=i+1;j<A.size();j++){
            for(int k=j+1;k<A.size();k++){
                int sum = A[i]+A[j]+A[k];
                int tar = target - sum;
                if(binary_search(A.begin()+k+1, A.end(), tar))
                    ans.insert({A[i],A[j],A[k],tar});
            }
        }
    }

    return vector<vector<int>>(ans.begin(), ans.end());
}