Академический Документы
Профессиональный Документы
Культура Документы
01h : 19m
Codenation - Campus Pool -15th October
to test end
N
Long Journey
Function Description
4
Complete the function findJourney in the editor below. The function must return an array
of integers, denoting the cities you will visit.
u: an array of n - 1 integers representing the first endpoint of the ith road
v: an array of n - 1 integers representing the second endpoint of the ith road
Constraints
2 ≤ n ≤ 105
1 ≤ u[i], v[i] ≤ 105 , where 0 ≤ i < n - 1
Locked stub code reads input from stdin and passes it to the function.
Sample Case 0
Sample Input 0
https://www.hackerrank.com/tests/b2aa5a9llnh/questions/foooqbnnlr 1/7
15/10/2018 Codenation - Campus Pool -15th October :: powered by HackerRank
4
3
2
1
1
3
1
3
4
Sample Output 0
2 1 3
Explanation 0
n = 4
u = [2, 1, 1]
v = [1, 3, 4]
There are 6 longest journeys: [2, 1, 3], [2, 1, 4], [3, 1, 2], [3, 1, 4], [4, 1, 2], [4, 1, 3]. [2, 1, 3]
is the lexicographically smallest.
Sample Case 1
Sample Input 1
5
4
1
2
3
4
4
2
3
4
5
Sample Output 1
1 2 3 4 5
https://www.hackerrank.com/tests/b2aa5a9llnh/questions/foooqbnnlr 2/7
15/10/2018 Codenation - Campus Pool -15th October :: powered by HackerRank
Explanation 1
n = 5
u = [1, 2, 3, 4]
v = [2, 3, 4, 5]
YOUR ANSWER
We recommend you take a quick tour of our editor before you proceed. The timer ✖
will pause up to 90 seconds for the tour. Start tour
1 ▸ #include ↔
2
3 using namespace std;
4
5 string ltrim(const string &);
6 string rtrim(const string &);
7
8
9 ▾ /*
10 * Complete the 'findJourney' function below.
11 *
12 * The function is expected to return an INTEGER_ARRAY.
13 * The function accepts following parameters:
14 * 1. INTEGER n
15 * 2. INTEGER_ARRAY u
16 * 3. INTEGER_ARRAY v
17 */
18
19 ▾ vector<int> findJourney(int n, vector<int> u, vector<int> v)
{vector<int>t;return t;
20
21 }
22
23
24 int main()
25 ▾ {
26 ofstream fout(getenv("OUTPUT_PATH"));
https://www.hackerrank.com/tests/b2aa5a9llnh/questions/foooqbnnlr 3/7
15/10/2018 Codenation - Campus Pool -15th October :: powered by HackerRank
27
28 string n_temp;
29 getline(cin, n_temp);
30
31 int n = stoi(ltrim(rtrim(n_temp)));
32
33 string u_count_temp;
34 getline(cin, u_count_temp);
35
36 int u_count = stoi(ltrim(rtrim(u_count_temp)));
37
38 vector<int> u(u_count);
39
40 ▾ for (int i = 0; i < u_count; i++) {
41 string u_item_temp;
42 getline(cin, u_item_temp);
43
44 int u_item = stoi(ltrim(rtrim(u_item_temp)));
45
46 ▾ u[i] = u_item;
47 }
48
49 string v_count_temp;
50 getline(cin, v_count_temp);
51
52 int v_count = stoi(ltrim(rtrim(v_count_temp)));
53
54 vector<int> v(v_count);
55
56 ▾ for (int i = 0; i < v_count; i++) {
57 string v_item_temp;
58 getline(cin, v_item_temp);
59
60 int v_item = stoi(ltrim(rtrim(v_item_temp)));
61
62 ▾ v[i] = v_item;
63 }
64
65 vector<int> result = findJourney(n, u, v);
66
67 ▾ for (int i = 0; i < result.size(); i++) {
68 ▾ fout << result[i];
69
70 ▾ if (i != result.size() - 1) {
71 fout << "\n";
72 }
73 }
74
https://www.hackerrank.com/tests/b2aa5a9llnh/questions/foooqbnnlr 4/7
15/10/2018 Codenation - Campus Pool -15th October :: powered by HackerRank
Line: 19 Col: 85
Test against custom input Run Code Submit code & Continue
Download sample test cases The input/output files have Unix line endings. Do not use
Notepad to edit them on windows.
https://www.hackerrank.com/tests/b2aa5a9llnh/questions/foooqbnnlr 5/7
15/10/2018 Codenation - Campus Pool -15th October :: powered by HackerRank
4
3
2
1
1
3
1
3
4
Your Output
2
1
3
5
4
1
2
3
4
4
2
3
4
5
Your Output
1
2
3
4
5
https://www.hackerrank.com/tests/b2aa5a9llnh/questions/foooqbnnlr 6/7
15/10/2018 Codenation - Campus Pool -15th October :: powered by HackerRank
https://www.hackerrank.com/tests/b2aa5a9llnh/questions/foooqbnnlr 7/7