You are given n points on a line with their coordinates x i. Find the point x so the sum of distances to the given points is minimal.
Input
The first line contains integer n (1 ≤ n ≤ 3·105) — the number of points on the line.
The second line contains n integers x i ( - 109 ≤ x i ≤ 109) — the coordinates of the given n points.
Output
Print the only integer x — the position of the optimal point on the line. If there are several optimal points print the position of the leftmost one. It is guaranteed that the answer is always the integer.Exampleinput
4
1 2 3 4
output
2
Solution:
#include <bits/stdc++.h>
using namespace std;
int a[1234567];
int main() {
int n;
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d", a + i);
}
sort(a, a + n);
printf("%d\n", a[(n - 1) / 2]);
return 0;
}
Related posts:
Wheels
Minimum spanning tree - Prim's algorithm
Treap (Cartesian tree)
Spectator Riots
Travel Card
Dijkstra on sparse graphs
Special Task
Captains Mode
Perfect Triples
Quest
Sherlock's bet to Moriarty
Linova and Kingdom
Little Artem and Grasshopper
Black and White Tree
Little Artem and Time Machine
Linear Diophantine Equation
Make It One
Little Artem and Matrix
New Year and Rating
Bogosort
Bear and Destroying Subtrees
A + B Strikes Back
Sherlock and his girlfriend
Lattice points inside non-lattice polygon
Power Tree
Lowest Common Ancestor - Farach-Colton and Bender Algorithm
Alyona and a Narrow Fridge
Koala and Notebook
Well-known Numbers
Palisection
Learning and Improving Algorithm through contests - Antti Laaksonen
Construct the String