-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
52 lines (45 loc) · 1.11 KB
/
main.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
#include <fstream>
#include <iostream>
#include <optional>
#include <unordered_set>
#include <vector>
std::vector<int> readFile();
std::optional<std::pair<int, int>> f1(const std::vector<int> &v, int sum)
{
std::unordered_set<int> set;
for (const auto &el : v) {
if (auto x = set.find(sum - el); x != set.end()) {
return std::make_pair(el, sum - el);
}
set.insert(el);
}
return std::nullopt;
}
std::optional<int> f2(const std::vector<int> &v, int sum)
{
std::unordered_set<int> set;
for (const auto &el : v) {
if (auto y = f1(v, sum - el); y) {
return y->first * y->second * el;
}
}
return std::nullopt;
}
std::vector<int> readFile()
{
std::ifstream input { "input.txt", std::ios::in };
int n;
input >> n;
std::vector<int> nums(n);
for (auto &el : nums)
input >> el;
return nums;
}
int main()
{
const auto v = readFile();
const auto r1 = f1(v, 2020);
std::cout << r1->first * r1->second << std::endl;
const auto r2 = f2(v, 2020);
std::cout << f2(v, 2020).value() << std::endl;
}