Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.FileNotFoundException;
- import java.io.FileReader;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.io.OutputStream;
- import java.io.PrintWriter;
- import java.util.StringTokenizer;
- import java.util.Arrays;
- public class OptimalPoint {
- static class InputReader
- {
- public BufferedReader reader;
- public StringTokenizer tok;
- public InputReader(InputStream inputStream)
- {
- reader=new BufferedReader(new InputStreamReader(inputStream));
- tok = null;
- }
- public InputReader(String inputFile) throws FileNotFoundException
- {
- reader = new BufferedReader(new FileReader(inputFile));
- tok=null;
- }
- public String nextLine()
- {
- String c="";
- try
- {
- c=reader.readLine();
- }
- catch (IOException e){
- throw new RuntimeException(e);
- }
- return c;
- }
- public String next()
- {
- while(tok ==null || !tok.hasMoreTokens())
- {
- try
- {
- tok=new StringTokenizer(nextLine());
- }
- catch(Exception e)
- {
- throw new RuntimeException(e);
- }
- }
- return tok.nextToken();
- }
- public int nextInt()
- {
- return Integer.parseInt(next());
- }
- public long nextLong()
- {
- return Long.parseLong(next());
- }
- }
- public static void main(String args[])
- {
- InputStream inputstream=System.in;
- OutputStream outputstream=System.out;
- InputReader in=new InputReader(inputstream);
- PrintWriter out=new PrintWriter (outputstream);
- Task solver=new Task();
- solver.solve(in,out);
- out.flush();
- }
- static class Task
- {
- public void solve(InputReader in,PrintWriter out)
- {
- int n=in.nextInt();
- long a[]=new long [n];
- for (int i=0 ;i<n ;i++)
- {
- a[i]=in.nextLong();
- }
- Arrays.sort(a);
- if (n%2!=0)
- {
- out.println(a[n/2]);
- }
- else
- {
- long suml=0,sumr=0;
- for (int i=0 ;i<n ;i++)
- {
- suml+=Math.abs(a[i]-a[n/2-1]);
- sumr+=Math.abs(a[i]-a[n/2]);
- }
- if(suml<=sumr)
- {
- out.println(a[n/2-1]);
- }
- else
- {
- out.println(a[n/2]);
- }
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment