Skip to content

Commit

Permalink
Merge pull request #112 from Shrishti-2002/main
Browse files Browse the repository at this point in the history
Added Leedcode problem-15(3sum) in cpp
  • Loading branch information
anupam-kumar-krishnan authored Oct 10, 2021
2 parents a11ed63 + c099045 commit 89c7a94
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions Coding Platforms(codechef,codeforces,etc)/LeetCode/3sum.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
class Solution {
public:
bool search(vector<int> v,int i,int j,int ele){
for(int p=i+1;p<j;p++){
if(v[p]==ele) return true;
}
return false;
}
vector<vector<int>> threeSum(vector<int>& nums) {
vector<vector<int>> v;
int n=nums.size();
if(n==0) return {};

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

for(int i=0;i<n-2;i++){
if(i>0 && nums[i]==nums[i-1]) continue;
for(int j=n-1;j>=(i+2);j--){
if(j<(n-1)&& nums[j]==nums[j+1]) continue;
int ele=(nums[i]+nums[j])*(-1);
if(ele>nums[j]) break;
if(binary_search(nums.begin()+i+1,nums.begin()+j,ele)) {
v.push_back({nums[i],ele,nums[j]});
}
}
}
return v;
}
};

0 comments on commit 89c7a94

Please sign in to comment.