import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuffer sb = new StringBuffer("");
int cases=Integer.parseInt(br.readLine());
for(int i=0;i<cases;i++) {
String str[]=br.readLine().split(" ");
int n=Integer.parseInt(str[0]);
int arr[]=new int[str.length-1];
for(int j=0;j<arr.length;j++){
arr[j]=Integer.parseInt(str[j+1]);
}
Arrays.sort(arr);
int sum=0;
int mid=0;
if(arr.length%2==1)
mid=arr[arr.length/2];
else
mid=(arr[arr.length/2]+arr[(arr.length+1)/2])/2;
for(int j=0;j<arr.length;j++){
sum+=Math.abs(mid-arr[j]);
}
sb.append(sum).append("\n");
}
System.out.print(sb);
}
}
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuffer sb = new StringBuffer("");
int cases=Integer.parseInt(br.readLine());
for(int i=0;i<cases;i++) {
String str[]=br.readLine().split(" ");
int n=Integer.parseInt(str[0]);
int arr[]=new int[str.length-1];
for(int j=0;j<arr.length;j++){
arr[j]=Integer.parseInt(str[j+1]);
}
Arrays.sort(arr);
int sum=0;
int mid=0;
if(arr.length%2==1)
mid=arr[arr.length/2];
else
mid=(arr[arr.length/2]+arr[(arr.length+1)/2])/2;
for(int j=0;j<arr.length;j++){
sum+=Math.abs(mid-arr[j]);
}
sb.append(sum).append("\n");
}
System.out.print(sb);
}
}
No comments:
Post a Comment