diff --git a/task_01/src/test.cpp b/task_01/src/test.cpp index ef5a86ae..b086e16e 100644 --- a/task_01/src/test.cpp +++ b/task_01/src/test.cpp @@ -3,6 +3,16 @@ #include "topology_sort.hpp" -TEST(TopologySort, Simple) { - ASSERT_EQ(1, 1); // Stack [] -} +TEST(find_pair_with_sum, Simple) { + std::vector test1 = {2, 3, 7, 12, 13, 28, 30, 60}; + + ASSERT_EQ(find_pair_with_sum(test1, 14), std::make_pair(0, 3)); + ASSERT_EQ(find_pair_with_sum(test1, 20), std::make_pair(2, 4)); + ASSERT_EQ(find_pair_with_sum(test1, 21), std::make_pair(-1, -1)); + ASSERT_EQ(find_pair_with_sum(test1, 58), std::make_pair(5, 6)); + ASSERT_EQ(find_pair_with_sum(test1, 70), std::make_pair(-1, -1)); + ASSERT_EQ(find_pair_with_sum(test1, -12312), std::make_pair(-1, -1)); + + std::vector test2 = {}; + ASSERT_EQ(find_pair_with_sum(test2, 1), std::make_pair(-1, -1)); +} \ No newline at end of file diff --git a/task_01/src/topology_sort.cpp b/task_01/src/topology_sort.cpp index e53f670c..6b3429e4 100644 --- a/task_01/src/topology_sort.cpp +++ b/task_01/src/topology_sort.cpp @@ -1 +1,22 @@ #include "topology_sort.hpp" + +std::pair find_pair_with_sum(std::vector mass, int a) { + if (!std::is_sorted(mass.begin(), mass.end()) or mass.size() == 0) { + return std::pair(-1, -1); + } + int L = 0; + int R = mass.size() - 1; + + while (L != R) { + if (mass[L] + mass[R] == a) { + return std::pair(L, R); + } + if (mass[L] + mass[R] < a) { + L += 1; + } else { + R -= 1; + } + } + + return std::pair(-1, -1); +} diff --git a/task_01/src/topology_sort.hpp b/task_01/src/topology_sort.hpp index 6f70f09b..033c4f5e 100644 --- a/task_01/src/topology_sort.hpp +++ b/task_01/src/topology_sort.hpp @@ -1 +1,6 @@ #pragma once + +#include +#include + +std::pair find_pair_with_sum(std::vector mass, int a); \ No newline at end of file