C++ | ⌛| USING 3 SUM | EASY EXPLANATION | BEGINNER FRIENDLY
UPVOTE AGAR SAWAL ASAAN KIYA AAPKE LIYE \uD83D\uDE42.\n# Advice\nDONT GO DIRECTLY TO SOLUTION.UNDERSTAND THE FLOW/APPROACH.\nYOU MUST BE KNOWING HOW TO SOLVE TW
UPVOTE AGAR SAWAL ASAAN KIYA AAPKE LIYE \uD83D\uDE42.\n# Advice\nDONT GO DIRECTLY TO SOLUTION.UNDERSTAND THE FLOW/APPROACH.\nYOU MUST BE KNOWING HOW TO SOLVE TWO SUM PROBLEM .\n\n# Intuition\nI HAVE SOLVED 3SUM USING 2 SUM .THAT\'S WHY I GOT INTUTION TO SOLVE FOUR SOME(\uD83D\uDE01) USING 3 SUM .\n\n# Approach\n1. IN AN ARRAY AT INDEX I, I TAKE THE ARRAY ELEMENT IN FOUR SUM CONSIDERATION IF NOT PREVIOUSLY TAKEN AND STORE IT IN A VARIABLE A=NUMS[I] .THEN ASK THREE SUM TO FIND TARGET-NUMS[I] IN REMAINING ARRAY.\n\n2. AGAIN IN ARRAY AT INDEX I, TAKE THE ARRAY ELEMENT IN FOUR SUM CONSIDERATION IF NOT PREVIOUSLY TAKEN AND STORE IT IN A VARIABLE B=NUMS[I].THEN ASK FOR TWO SUM TO FIND TARGET-NUMS[I] IN REMAINING ARRAY.\n\n3. SIMPLE TWO SUM PROBLEM\uD83D\uDE0E.\n\n```\nwhile(i<j){\n            if(target==nums[i]+nums[j]){\n                //STORE IN ANS \n                ans.push_back({a,b,nums[i],nums[j]});\n        // DISTINCT SUBARRAY SHOULD BE INSIDE VECTOR<VECTOR<INT>>ANS.\n                i++,j--;\n                while(j>i && nums[j]==nums[j+1])j--;\n                while(i<j&&nums[i]==nums[i-1])i++;\n            }\n            else if(nums[i]+nums[j]>target)j--;\n            else{\n                ++i;\n            }\n        }\n\n```\nEXAMPLE : LET ARRAY[-2,-2,-1,0,1,1,2,2].TARGET=0.\n![WhatsApp Image 2023-04-21 at 12.06.57.jpg](https://assets.leetcode.com/users/images/13d0d6d0-ab45-404e-a022-9768e9af6854_1682059109.5560734.jpeg)\n\n# Code\n```\nclass Solution {\npublic:\n    void twosum(vector<int>&nums,int i,long long target,vector<vector<int>>&ans,int a ,int b){\n        int n =nums.size();\n        int j=n-1;\n\n        while(i<j){\n            if(target==nums[i]+nums[j]){\n                ans.push_back({a,b,nums[i],nums[j]});\n                i++,j--;\n                while(j>i && nums[j]==nums[j+1])j--;\n                while(i<j&&nums[i]==nums[i-1])i++;\n            }\n            else if(nums[i]+nums[j]>target)j--;\n            else{\n                ++i;\n            }\n        }\n        return;\n    }\n    void threesum(vector<vector<int>>&ans,vector<int>& nums,int j,long long target,int a ) {\n        int n =nums.size();\n        twosum(nums,j+1,target-nums[j],ans,a,nums[j]);\n\n        for(int i =j+1;i<n-2;i++){\n            if(nums[i]==nums[i-1])continue;\n            int b=nums[i];\n            twosum(nums,i+1,target-nums[i],ans,a,b);\n        }\n        return ;\n    }\n    vector<vector<int>> fourSum(vector<int>& nums, int t) {\n        long long target=t;\n        vector<vector<int>>ans;\n        sort(nums.begin(),nums.end());\n        int n =nums.size();\n\n        if(n>=4)threesum(ans,nums,1,target-nums[0],nums[0]);\n        if(nums[0]>0 && nums[0]>target)return ans;\n\n        for(int i=1;i<n-3;i++){\n            if(nums[i]==nums[i-1])continue;\n            int a=nums[i];\n            threesum(ans,nums,i+1,target-nums[i],a);\n        }\n        return ans ;\n    }\n};\n```\nTHANK YOU \nCODE BY:) AMAN MAURYA
['C++']