diff --git a/sandbox/template/src/main b/sandbox/template/src/main new file mode 100755 index 00000000..9342dd8d Binary files /dev/null and b/sandbox/template/src/main differ diff --git a/sandbox/template/src/main.cpp b/sandbox/template/src/main.cpp index 0e4393ba..0a554f65 100644 --- a/sandbox/template/src/main.cpp +++ b/sandbox/template/src/main.cpp @@ -1,3 +1,6 @@ #include -int main() { return 0; } +int main() { + std::cout << "Hello world!"; + return 0; +} diff --git a/task_01/src/main.cpp b/task_01/src/main.cpp index 0e4393ba..7825a349 100644 --- a/task_01/src/main.cpp +++ b/task_01/src/main.cpp @@ -1,3 +1,22 @@ #include -int main() { return 0; } +#include "solution.h" + +int main() { + int sum, arr_size, t; + std::vector input_vector; + std::unordered_map indices_map; // keeps array numbers as keys and + // indices as values behind keys + + std::cin >> sum >> arr_size; + + for (int i = 0; i < arr_size; i++) { + std::cin >> t; + input_vector.push_back(t); + } + + std::pair sol = solution(sum, input_vector); + std::cout << sol.first << ' ' << sol.second; + + return 0; +} \ No newline at end of file diff --git a/task_01/src/solution.h b/task_01/src/solution.h new file mode 100644 index 00000000..7803b04e --- /dev/null +++ b/task_01/src/solution.h @@ -0,0 +1,36 @@ +#pragma once +#include +#include + +/* + +Output - indices of two numbers, which sum is equal to needed number, if there's +no such numbers, the output is "-1 -1" + +Input: + +10 +10 +-2 2 3 3 5 8 11 13 14 15 + +Output: + +1 5 + +*/ + +// Solution below has a time complexity of O(n) and memory complexity of O(1) + +std::pair solution(int sum, std::vector vector) { + if (vector.size() < 2) return {-1, -1}; + int left_pointer = 0, right_pointer = vector.size() - 1; + while (vector[left_pointer] < vector[right_pointer]) { + if (vector[left_pointer] + vector[right_pointer] > sum) + right_pointer--; // move right pointer to the left by one + else if (vector[left_pointer] + vector[right_pointer] < sum) + left_pointer++; // move left pointer to the right by one + else + return {left_pointer, right_pointer}; + } + return {-1, -1}; // in case there are no such numbers +} \ No newline at end of file diff --git a/task_01/src/test.cpp b/task_01/src/test.cpp index ef5a86ae..36bc7436 100644 --- a/task_01/src/test.cpp +++ b/task_01/src/test.cpp @@ -1,8 +1,30 @@ #include -#include "topology_sort.hpp" +#include "solution.h" -TEST(TopologySort, Simple) { - ASSERT_EQ(1, 1); // Stack [] -} +TEST(solution, simple) { + std::vector v1 = {-2, 2, 3, 3, 5, 9, 11, 13, 14, 15}; + std::pair p1 = {-1, -1}; + ASSERT_EQ(p1, solution(10, v1)); + + std::vector v2 = {-2, 2, 3, 3, 5, 8, 11, 13, 14, 15}; + std::pair p2 = {1, 5}; + ASSERT_EQ(p2, solution(10, v2)); + + std::vector v3 = {}; + std::pair p3 = {-1, -1}; + ASSERT_EQ(p3, solution(0, v3)); + + std::vector v4 = {1}; + std::pair p4 = {-1, -1}; + ASSERT_EQ(p4, solution(1, v4)); + + std::vector v5 = {1, 2}; + std::pair p5 = {0, 1}; + ASSERT_EQ(p5, solution(3, v5)); + + std::vector v6 = {1, 2, 2, 2, 4, 5, 6, 7, 8}; + std::pair p6 = {0, 5}; + ASSERT_EQ(p6, solution(6, v6)); +} \ No newline at end of file diff --git a/task_01/src/topology_sort.cpp b/task_01/src/topology_sort.cpp deleted file mode 100644 index e53f670c..00000000 --- a/task_01/src/topology_sort.cpp +++ /dev/null @@ -1 +0,0 @@ -#include "topology_sort.hpp" diff --git a/task_01/src/topology_sort.hpp b/task_01/src/topology_sort.hpp deleted file mode 100644 index 6f70f09b..00000000 --- a/task_01/src/topology_sort.hpp +++ /dev/null @@ -1 +0,0 @@ -#pragma once diff --git a/testfile.txt b/testfile.txt new file mode 100644 index 00000000..e69de29b