#include <stdio.h> #include <stdlib.h> #include <math.h> int closest_element(int *arr, float avg, int n); int main(void) { int n, *arr, count = 0; scanf("%d", &n); arr = (int *)malloc(sizeof(int) * n); for (int i = 0; i < n; i++) { scanf("%d", &arr[i]); count += arr[i]; } float avg = (float)count / n; printf("%d", closest_element(arr, avg, n)); return 0; } int closest_element(int *arr, float avg, int n) { int value; float min = 100000.0; for (int i = 0; i < n; i++) { if (min == fabs((float)arr[i] - avg)) { if (arr[i] > value) { value = arr[i]; } } if (min > fabs((float)arr[i] - avg)) { min = fabs((float)arr[i] - avg); value = arr[i]; } } return value; }

15,845,742 members