Friday, November 29, 2013

UVA 10107 : What is the Median?


import java.util.*;
public class Median
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
ArrayList<Integer> al = new ArrayList<Integer>();
int n = 0;
while(sc.hasNext())
{
n = sc.nextInt();
al.add(n);
Collections.sort(al);
if( al.size() % 2 == 0)
{
int pos1 = al.size()/2;
int pos2 = pos1 - 1;
int ans = (al.get(pos1) + al.get(pos2)) / 2;
System.out.println(ans);
}
else
{
int pos1 = al.size()/2;
int ans = al.get(pos1);
System.out.println(ans);
}
}
}
}
view raw gistfile1.txt hosted with ❤ by GitHub