forked from Nanduag0/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 3
/
3sumproblem.txt
55 lines (54 loc) · 1 KB
/
3sumproblem.txt
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
55
#include <iostream>
using namespace std;
#include<bits/stdc++.h>
int main()
{
int n;
cin>>n;
vector<int> vec(n);
int a;
for(int i=0;i<n;i++)
{
cin>>a;
vec[i]=a;
}
int sum=0,count=0;
cin>>sum;
sort(vec.begin(),vec.end());
for(int i=0;i<n-1;i++)
{
if(i>0 && vec[i]==vec[i-1])
continue;
int l=i+1;
int r=n-1;
while(l<=r)
{
if(vec[l]+vec[r]+vec[i]==sum)
{
cout<<vec[l]<<" "<<vec[r]<<" "<<vec[i]<<"\n";
count++;
int front=vec[l];
while(l<n && vec[l]==front)
{
l++;
}
int rear=vec[r];
while(r<n && vec[r]==rear)
{
r--;
}
l++;
r--;
}
else
if(vec[l]+vec[r]+vec[i]<sum)
{
l++;
}
else
r--;
}
}
cout<<count<<" ";
return 0;
}