//Problem link>>http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=982
#include <bits/stdc++.h>
using namespace std ;
int main ()
{
int t , i ,r , a[1000] ,mid ;
cin>>t ;
while (t--)
{
cin>>r ;
for (i=0 ; i< r ; i++) cin>>a[i] ;
sort (a , a+r) ;
if (r%2==1) mid =a[r/2] ; // as array starts with 0
else mid = a[r/2-1] ;
int sum=0 ;
for (i=0 ; i<r ; i++) sum=sum+ abs(mid-a[i]) ;
cout<<sum<<endl ;
}
return 0 ;
}
#include <bits/stdc++.h>
using namespace std ;
int main ()
{
int t , i ,r , a[1000] ,mid ;
cin>>t ;
while (t--)
{
cin>>r ;
for (i=0 ; i< r ; i++) cin>>a[i] ;
sort (a , a+r) ;
if (r%2==1) mid =a[r/2] ; // as array starts with 0
else mid = a[r/2-1] ;
int sum=0 ;
for (i=0 ; i<r ; i++) sum=sum+ abs(mid-a[i]) ;
cout<<sum<<endl ;
}
return 0 ;
}
No comments:
Post a Comment