Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Task 01 fixed x2 #23

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added sandbox/template/src/main
Binary file not shown.
5 changes: 4 additions & 1 deletion sandbox/template/src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#include <iostream>

int main() { return 0; }
int main() {
std::cout << "Hello world!";
return 0;
}
21 changes: 20 additions & 1 deletion task_01/src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,22 @@
#include <iostream>

int main() { return 0; }
#include "solution.h"

int main() {
int sum, arr_size, t;
std::vector<int> input_vector;
std::unordered_map<int, int> 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<int, int> sol = solution(sum, input_vector);
std::cout << sol.first << ' ' << sol.second;

return 0;
}
36 changes: 36 additions & 0 deletions task_01/src/solution.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#pragma once
#include <unordered_map>
#include <vector>

/*

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<int, int> solution(int sum, std::vector<int> 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
}
30 changes: 26 additions & 4 deletions task_01/src/test.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,30 @@

#include <gtest/gtest.h>

#include "topology_sort.hpp"
#include "solution.h"

TEST(TopologySort, Simple) {
ASSERT_EQ(1, 1); // Stack []
}
TEST(solution, simple) {
std::vector<int> v1 = {-2, 2, 3, 3, 5, 9, 11, 13, 14, 15};
std::pair<int, int> p1 = {-1, -1};
ASSERT_EQ(p1, solution(10, v1));

std::vector<int> v2 = {-2, 2, 3, 3, 5, 8, 11, 13, 14, 15};
std::pair<int, int> p2 = {1, 5};
ASSERT_EQ(p2, solution(10, v2));

std::vector<int> v3 = {};
std::pair<int, int> p3 = {-1, -1};
ASSERT_EQ(p3, solution(0, v3));

std::vector<int> v4 = {1};
std::pair<int, int> p4 = {-1, -1};
ASSERT_EQ(p4, solution(1, v4));

std::vector<int> v5 = {1, 2};
std::pair<int, int> p5 = {0, 1};
ASSERT_EQ(p5, solution(3, v5));

std::vector<int> v6 = {1, 2, 2, 2, 4, 5, 6, 7, 8};
std::pair<int, int> p6 = {0, 5};
ASSERT_EQ(p6, solution(6, v6));
}
1 change: 0 additions & 1 deletion task_01/src/topology_sort.cpp

This file was deleted.

1 change: 0 additions & 1 deletion task_01/src/topology_sort.hpp

This file was deleted.

Empty file added testfile.txt
Empty file.
Loading