Skip to content

Instantly share code, notes, and snippets.

@peter279k
Created December 22, 2015 01:55
Show Gist options
  • Save peter279k/f318160f4280b07be859 to your computer and use it in GitHub Desktop.
Save peter279k/f318160f4280b07be859 to your computer and use it in GitHub Desktop.
import java.util.*;
public class main{
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int teams = input.nextInt();
while(input.hasNext()) {
for(int i=1;i<=teams;i++) {
int nums = input.nextInt();
int number[] = new int[nums];
for(int j=0;j<nums;j++) {
number[j] = input.nextInt();
}
Arrays.sort(number);
int mid = 0;
int result = 0;
if(nums % 2 == 0) {
mid = number[(nums + 1) / 2 - 1] + number[(nums + 1) / 2];
mid /= 2;
}
else {
mid = number[(nums - 1) / 2];
}
for(int j=0;j<number.length;j++) {
result += Math.abs(mid - number[j]);
}
System.out.println(result);
}
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment