-
Notifications
You must be signed in to change notification settings - Fork 93
/
Copy path3sum.cpp
54 lines (49 loc) · 1.41 KB
/
3sum.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
class Solution {
public:
vector<vector<int>> threeSum(vector<int>& arr) {
bool found = false;
vector<int> vect;
vector<vector<int>> ans;
// sort array elements
sort(arr.begin(), arr.end());
int n=arr.size();
int i=0;
while(i<n-2)
{
// initialize left and right
int l = i + 1;
int r = n - 1;
int x = arr[i];
while (l < r)
{
int low=arr[l];
int high=arr[r];
if (x + arr[l] + arr[r] == 0)
{
// print elements if it's sum is zero
vect.clear();
vect.push_back(x);
vect.push_back(arr[l]);
vect.push_back(arr[r]);
ans.push_back(vect);
while(l<r && arr[l]==low)l++;
while(r>l && arr[r]==high)r--;
}
// If sum of three elements is less
// than zero then increment in left
else if (x + arr[l] + arr[r] < 0)
{
while(l<r && arr[l]==low)l++;
}
// if sum is greater than zero than
// decrement in right side
else
{
while(r>l && arr[r]==high)r--;
}
}
while(i<n-2 && arr[i]==x)i++;
}
return ans;
}
};