Created
December 7, 2019 08:36
-
-
Save nahiyan/039ff669917bf7dce3592edba2e30706 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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