-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem_1.cpp
More file actions
30 lines (22 loc) · 721 Bytes
/
problem_1.cpp
File metadata and controls
30 lines (22 loc) · 721 Bytes
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
// Given a list of numbersand a number k, return whether any two numbers from the list add up to k.
// For example, given [10, 15, 3, 7] and k of 17, return true since 10 + 7 is 17.
// Bonus: Can you do this in one pass?
// Example
// int array[4] = {2, 2, 2, 3};
// const auto size = sizeof(array) / sizeof(*array);
// auto result = problem_one(5, array, size);
#include <set>
using namespace std;
bool problem_one(const int sum, int const numbers[], const unsigned int size)
{
set<int> set;
for (unsigned int i = 0; i < size; i++)
{
const auto possible_answer = sum - numbers[i];
for (const auto& item : set)
if (item == numbers[i])
return true;
set.insert(possible_answer);
}
return false;
}