Skip to content

Instantly share code, notes, and snippets.

@nahiyan
Created December 7, 2019 08:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nahiyan/039ff669917bf7dce3592edba2e30706 to your computer and use it in GitHub Desktop.
Save nahiyan/039ff669917bf7dce3592edba2e30706 to your computer and use it in GitHub Desktop.
#include <stdio.h>
int abs(int value) {
if (value < 0)
return value * -1;
else
return value;
}
int even(int value) {
if (value % 2 == 0)
return 1;
return 0;
}
int median(int s[], int n) {
if (even(n))
{
return (s[(n / 2) - 1] + s[(n / 2)]) / 2;
}
else
{
return s[(n / 2)];
}
}
int main()
{
int n;
scanf("%d", &n);
int d[n];
int i = 0;
while (i < n)
{
int m;
scanf("%d", &m);
int s[m];
int j = 0;
while (j < m)
{
scanf("%d", &s[j]);
j++;
}
d[i] = 0;
j = 0;
while (j < m)
{
d[i] += abs(s[j] - median(s, m));
j++;
}
i++;
}
i = 0;
while (i < n)
{
printf("%d\n", d[i]);
i++;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment