-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay1part2.cpp
More file actions
70 lines (58 loc) · 1.22 KB
/
Copy pathDay1part2.cpp
File metadata and controls
70 lines (58 loc) · 1.22 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
int main()
{
fstream indata;
indata.open("day1data");
vector<int> v;
int p;
if (indata.fail())
{
cout << "some thing is wrong";
}
while (indata >> p)
{
v.push_back(p);
}
sort(v.begin(), v.end());
int l = v.size() - 1;
int j=l;
int count=1;
int i;
int a=0;
int b=0;
int c=0;
for(int k=0;k<=l;k++)
{
int sum=2020-v[k];
i=k+1;//because in previous value of k i which are less than k combinations are already` covered
j=l;
while (i < j&&j!=k)
{
if (v[i] + v[j] == sum)//using same logic for sum of 2 numbers;
{
a=i;
b=j;
c=k;
break;
count=0;
}
else if (v[i] + v[j] < sum)
{
i++;
}
else if (v[i] + v[j] > sum)
{
j--;
}
}
if(count==0)
{
break;
}
}
cout << v[a]<<endl<<v[b]<<endl<<v[c] << endl;
return 0;
}