diff --git a/CPP/Problems/Leetcode 15-3SumProblem.cpp b/CPP/Problems/Leetcode 15-3SumProblem.cpp new file mode 100644 index 00000000..41b46a51 --- /dev/null +++ b/CPP/Problems/Leetcode 15-3SumProblem.cpp @@ -0,0 +1,45 @@ +// Problem link:https://leetcode.com/problems/3sum/description/ + +class Solution { +public: + vector> threeSum(vector& nums) { + sort(nums.begin(),nums.end()); + int n=nums.size(); + vector> ans; + set> st; + for(int i=0;i check; + int find=-(nums[i]); + int l=i+1,r=n-1; + while(r>l){ + int sum=(nums[l]+nums[r]); + if(sum==find){ + check.push_back(nums[i]); + check.push_back(nums[l]); + check.push_back(nums[r]); + st.insert(check); + if(check.size()==3){ + check.clear(); + } + l++;r--; + } + else if(sum>find){ + r--; + } + else{ + l++; + } + } + } + + for(auto el:st){ + vector check1; + for(int i=0;i