Skip to content

Commit 70b39e9

Browse files
committed
task_01 returned
1 parent 9d53c25 commit 70b39e9

File tree

5 files changed

+144
-0
lines changed

5 files changed

+144
-0
lines changed

task_01/CMakeLists.txt

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
cmake_minimum_required(VERSION 3.10)
2+
3+
get_filename_component(PROJECT_NAME ${CMAKE_CURRENT_LIST_DIR} NAME)
4+
string(REPLACE " " "_" PROJECT_NAME ${PROJECT_NAME})
5+
project(${PROJECT_NAME} C CXX)
6+
7+
set(CMAKE_CXX_STANDARD 20)
8+
set(CMAKE_CXX_STANDARD_REQUIRED ON)
9+
10+
file(GLOB_RECURSE source_list "src/*.cpp" "src/*.hpp")
11+
file(GLOB_RECURSE lib_source_list "../lib/src/*.cpp" "../lib/src/*.hpp")
12+
file(GLOB_RECURSE main_source_list "src/main.cpp")
13+
file(GLOB_RECURSE test_source_list "src/*.cpp")
14+
file(GLOB_RECURSE test_list "src/*test.cpp")
15+
16+
list(REMOVE_ITEM test_source_list ${main_source_list})
17+
list(REMOVE_ITEM source_list ${test_list})
18+
19+
include_directories(${PROJECT_NAME} PUBLIC src)
20+
include_directories(${PROJECT_NAME} PUBLIC ../lib/src)
21+
22+
add_executable(${PROJECT_NAME} ${source_list})
23+
target_link_libraries(${PROJECT_NAME} PUBLIC Utils)
24+
25+
# Locate GTest
26+
enable_testing()
27+
find_package(GTest REQUIRED)
28+
include_directories(${GTEST_INCLUDE_DIRS})
29+
30+
# Link runTests with what we want to test and the GTest and pthread library
31+
add_executable(${PROJECT_NAME}_tests ${test_source_list})
32+
target_link_libraries(
33+
${PROJECT_NAME}_tests
34+
GTest::gtest_main
35+
Utils
36+
)
37+
38+
include(GoogleTest)
39+
gtest_discover_tests(${PROJECT_NAME}_tests)

task_01/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Задача 1
2+
3+
Дано целое число и массив целых чисел, нужно найти 2 числа из массива которые в сумме дадут заданное число

task_01/src/main.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#include <iostream>
2+
3+
#include "solution.h"
4+
5+
int main() {
6+
int sum, arr_size, t;
7+
std::vector<int> input_vector;
8+
std::unordered_map<int, int> indices_map; // keeps array numbers as keys and
9+
// indices as values behind keys
10+
11+
std::cin >> sum >> arr_size;
12+
13+
for (int i = 0; i < arr_size; i++) {
14+
std::cin >> t;
15+
input_vector.push_back(t);
16+
}
17+
18+
std::pair<int, int> sol = solution(sum, input_vector);
19+
std::cout << sol.first << ' ' << sol.second;
20+
21+
return 0;
22+
}

task_01/src/solution.h

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#include <unordered_map>
2+
#include <vector>
3+
4+
/*
5+
6+
Output - indices of two numbers, which sum is equal to needed number, if there's
7+
no such numbers, the output is "-1 -1"
8+
9+
Input:
10+
11+
10
12+
10
13+
-2 2 3 3 5 8 11 13 14 15
14+
15+
Output:
16+
17+
1 5
18+
19+
*/
20+
21+
// Solution below has a time complexity of O(n) and memory complexity of O(n)
22+
23+
std::pair<int, int> solution(int sum, std::vector<int> v) {
24+
std::unordered_map<int, int> indices_map; // keeps array numbers as keys and
25+
// indices as values behind keys
26+
27+
for (int i = 0; i < v.size(); i++) {
28+
if (indices_map.find(sum - v[i]) !=
29+
indices_map
30+
.end()) { // if key "number - t" exists, we have found the solution
31+
return {indices_map[sum - v[i]], i};
32+
}
33+
34+
if (indices_map.find(v[i]) == indices_map.end())
35+
indices_map[v[i]] =
36+
i; // We only add keys that weren't in the map before (that
37+
// way we get the least possible sum of i and j)
38+
}
39+
40+
return {-1, -1}; // in case there are no such numbers
41+
}

task_01/src/test.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
2+
#include <gtest/gtest.h>
3+
4+
#include "solution.h"
5+
6+
TEST(solution, simple) {
7+
std::vector<int> v1 = {-2, 2, 3, 3, 5, 9, 11, 13, 14, 15};
8+
std::pair<int, int> p1 = {-1, -1};
9+
ASSERT_EQ(p1, solution(10, v1));
10+
11+
std::vector<int> v2 = {-2, 2, 3, 3, 5, 8, 11, 13, 14, 15};
12+
std::pair<int, int> p2 = {1, 5};
13+
ASSERT_EQ(p2, solution(10, v2));
14+
15+
std::vector<int> v3 = {};
16+
std::pair<int, int> p3 = {-1, -1};
17+
ASSERT_EQ(p3, solution(0, v3));
18+
19+
std::vector<int> v4 = {1};
20+
std::pair<int, int> p4 = {-1, -1};
21+
ASSERT_EQ(p4, solution(1, v4));
22+
23+
std::vector<int> v5 = {1, 2};
24+
std::pair<int, int> p5 = {0, 1};
25+
ASSERT_EQ(p5, solution(3, v5));
26+
27+
// if there are multiple solutions, the algorithm
28+
// will pick the one, in which sum of i and j is the least,
29+
// where i and j are indices of numbers, which sum is equal to needed number
30+
std::vector<int> v6 = {1, 1, 1, 1, 1, 1, 1, 1, 1};
31+
std::pair<int, int> p6 = {0, 1};
32+
ASSERT_EQ(p6, solution(2, v6));
33+
34+
// in case there are multiple solutions in which i+j is the least,
35+
// the algorithm will pick the one, in which i is greater
36+
std::vector<int> v7 = {1, 2, 1, 1, 4, 5, 1, 1, 1};
37+
std::pair<int, int> p7 = {1, 4};
38+
ASSERT_EQ(p7, solution(6, v7));
39+
}

0 commit comments

Comments
 (0)