Sie sind auf Seite 1von 6

15/10/2018 Codenation - Campus Pool -15th October :: powered by HackerRank

 
  01h : 21m
Codenation - Campus Pool -15th October
to test end

N

 Guards fighting

In a utopian future, the king of Iowa has a long palace built in the air with some podiums of
same height created horizontally (all podiums are at different positions). The king wants to
1
place some guards on the podiums maximizing the separation between them. You should
find the smallest separation in the optimal placement.
2
 
For example, consider the positions of n = 6 podiums are given by podiumPositions = [1, 6,
3
11, 20, 30, 45] and we want to place g = 5 guards maximizing the separation. The optimal
placement is [1, 11, 20, 30, 45], therefore, the smallest separation is 20 - 11 = 9
4
 
Function Description
 
Complete the function minimumGuardsDistance in the editor below. The function must
return an integer, the minimum distance between guards when placed optimally.
 
minimumGuardsDistance has the following parameter(s):
    podiumPositions: an array of n integers, the position of podiums
    g: an integer, the number of guards
 
Constraints
2 ≤ n ≤ 105
0 ≤ podiumPositioni ≤ 109 (0 ≤ i < n, podiumPositioni is the position of podium at
position i)
2≤g≤n
 

Input Format For Custom Testing

The first line contains an integer n, the number of podiums.


n lines follow, each listing a position of the podium.
The last line contains an integer g, the number of guards.

Sample Case 0

Sample Input For Custom Testing

6
1
6

https://www.hackerrank.com/tests/b2aa5a9llnh/questions/ga21hfr5go 1/6
15/10/2018 Codenation - Campus Pool -15th October :: powered by HackerRank

11
20
30
45
5

Sample Output

Explanation
The optimal placement is [1, 11, 20, 30, 45], therefore, the smallest separation is 20 - 11
=9

Sample Case 1

Sample Input For Custom Testing

5
3
5
6
10
14
3

Sample Output

Explanation
The optimal positions for the guards to stand are [3, 10, 14]

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

Draft saved 09:35 pm Original code C++  ⚙

1 ▸ #include ↔
2
3 using namespace std;
4
5 string ltrim(const string &);
6 string rtrim(const string &);
7

https://www.hackerrank.com/tests/b2aa5a9llnh/questions/ga21hfr5go 2/6
15/10/2018 Codenation - Campus Pool -15th October :: powered by HackerRank

8
9 ▾ /*
10 * Complete the 'minimumGuardsDistance' function below.
11 *
12 * The function is expected to return an INTEGER.
13 * The function accepts following parameters:
14 * 1. INTEGER_ARRAY podiumPostions
15 * 2. INTEGER g
16 */
17
18 ▾ int minimumGuardsDistance(vector<int> podiumPostions, int g) {
return 0;
19
20 }
21
22
23 int main()
24 ▾ {
25    ofstream fout(getenv("OUTPUT_PATH"));
26
27    string podiumPostions_count_temp;
28    getline(cin, podiumPostions_count_temp);
29
30    int podiumPostions_count =
stoi(ltrim(rtrim(podiumPostions_count_temp)));
31
32    vector<int> podiumPostions(podiumPostions_count);
33
34 ▾    for (int i = 0; i < podiumPostions_count; i++) {
35        string podiumPostions_item_temp;
36        getline(cin, podiumPostions_item_temp);
37
38        int podiumPostions_item =
stoi(ltrim(rtrim(podiumPostions_item_temp)));
39
40 ▾        podiumPostions[i] = podiumPostions_item;
41   }
42
43    string g_temp;
44    getline(cin, g_temp);
45
46    int g = stoi(ltrim(rtrim(g_temp)));
47
48    int result = minimumGuardsDistance(podiumPostions, g);
49
50    fout << result << "\n";
51

https://www.hackerrank.com/tests/b2aa5a9llnh/questions/ga21hfr5go 3/6
15/10/2018 Codenation - Campus Pool -15th October :: powered by HackerRank

52    fout.close();
53
54    return 0;
55 }
56
57 ▾ string ltrim(const string &str) {
58    string s(str);
59
60    s.erase(
61        s.begin(),
62        find_if(s.begin(), s.end(), not1(ptr_fun<int, int>
(isspace)))
63   );
64
65    return s;
66 }
67
68 ▾ string rtrim(const string &str) {
69    string s(str);
70
71    s.erase(
72        find_if(s.rbegin(), s.rend(), not1(ptr_fun<int, int>
(isspace))).base(),
73        s.end()
74   );
75
76    return s;
77 }
78

Line: 18 Col: 73

Test against custom input Run Code Submit code & Continue

(You can submit any number of times)

 Download sample test cases The input/output files have Unix line endings. Do not use
Notepad to edit them on windows.

Status: No test cases passed.


 Tip: Debug your code against custom input

Testcase 1: Wrong Answer


Input [ Download]

https://www.hackerrank.com/tests/b2aa5a9llnh/questions/ga21hfr5go 4/6
15/10/2018 Codenation - Campus Pool -15th October :: powered by HackerRank

6
1
6
11
20
30
45
5

Your Output

Expected Output [ Download]

Testcase 2: Wrong Answer


Input [ Download]

5
3
5
6
10
14
3

Your Output

Expected Output [ Download]

Testcase 3: Wrong Answer


Input [ Download]

10
11
45
53
116
131
149
201
205
245

https://www.hackerrank.com/tests/b2aa5a9llnh/questions/ga21hfr5go 5/6
15/10/2018 Codenation - Campus Pool -15th October :: powered by HackerRank

282
6

Your Output

Expected Output [ Download]

37

About Privacy Policy Terms of Service

https://www.hackerrank.com/tests/b2aa5a9llnh/questions/ga21hfr5go 6/6

Das könnte Ihnen auch gefallen