UVA – 10041 – Vito’s Family

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;

public class Main {
    public static void main(String[] args) throws NumberFormatException, 
                            IOException {
        BufferedReader bf = new BufferedReader(new InputStreamReader(
                            System.in));

        int  T = Integer.parseInt(bf.readLine());
        while (T--!=0) {
            StringTokenizer tokens = new StringTokenizer(bf.readLine());
            int r =Integer.parseInt(tokens.nextToken());
            int[] rel = new int[r];
            while (--r !=-1)
                rel[r] = Integer.parseInt(tokens.nextToken());
            Arrays.sort(rel);

            r = rel.length;
            int sum = 0, median = r%2==0 ? (rel[r/2] + rel[r/2 -1])/2 : 
                      rel[r/2];
            for (int i=0; i<rel.length; i++)
                sum += Math.abs(rel[i] -median) ;

            System.out.println(sum);
        }
    }
}
Advertisements

One thought on “UVA – 10041 – Vito’s Family

  1. Anup says:

    thanks a lot………..

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: