-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhyperspace-travel.cpp
More file actions
35 lines (30 loc) · 948 Bytes
/
hyperspace-travel.cpp
File metadata and controls
35 lines (30 loc) · 948 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// Author: btjanaka (Bryon Tjanaka)
// Problem: (HackerRank) hyperspace-travel
// Title: Hyperspace Travel
// Link: https://www.hackerrank.com/challenges/hyperspace-travel/problem
// Idea: Take the median of each dimension -- when n is even, take the lower of
// the two middle values rather than averaging them.
// Difficulty: medium
// Tags: geometry
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m;
cin >> n >> m;
// Input the values for each dimension.
vector<vector<int>> dims(m);
int x;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
cin >> x;
dims[j].push_back(x);
}
}
// Sort each dimension and output the median of each dimension.
for (int i = 0; i < m; ++i) sort(dims[i].begin(), dims[i].end());
for (int i = 0; i < m; ++i) {
int median = n % 2 == 1 ? dims[i][n / 2] : dims[i][n / 2 - 1];
cout << median << (i == m - 1 ? '\n' : ' ');
}
return 0;
}