CPE 2. Vito's family

 avatar
user_3763047219
c_cpp
2 years ago
689 B
3
Indexable
#include <iostream>
using namespace std;
#include <string>
#include <math.h>
#include <algorithm>

int main(){
    int n;
    cin >> n;
    for(int i=0;i<n;i++){
        int relative=0;
        cin >> relative;
        int home[relative];
        for(int j=0;j<relative;j++){
            cin >> home[j];
        }
        sort(home,home+relative);
        int mid;
        if(relative%2==0){
            mid=(home[(relative/2)-1]+home[relative/2])/2;
        }
        else{
            mid=home[relative/2];
        }
        int sum=0;
        for(int j=0;j<relative;j++){
            sum=sum+abs(home[j]-mid);
        }
        cout<<sum<<endl;

    }

}
Editor is loading...