Mayores que la media#7833

Mayores que la media (Editorial)

First, compute the sum of the given numbers. The arithmetic mean of all the numbers is . Next, find the sum and the count of numbers greater than the arithmetic mean.

Example

In the example, n = 5 numbers are given. Their sum is s = 1 + 6 + 2 + 6 + 3 = 18. The arithmetic mean of the numbers is s / n = 18 / 5 = 3.6. There are two numbers greater than : these are the two sixes. Their sum is .

Algorithm implementation

Read the input array. Compute the sum of its elements.

scanf("%d",&n);
for(i = 0; i < n; i++)
{
  scanf("%d",&m[i]);
  s += m[i];
}

Compute the sum and the count of the elements in the array that are greater than the arithmetic mean . Note that the inequality m[i] > s / n is equivalent to m[i] \cdot n > s.

sum = cnt = 0;
for(i = 0; i < n; i++)
  if (m[i] * n > s) // m[i] > s / n
  {
    sum += m[i];
    cnt++;
  }

Print the answer.

printf("%d %d\n",sum,cnt);

Python implementation

Read the input data.

n = int(input())
lst = list(map(int, input().split()))

Compute the arithmetic mean of the list.

ave = sum(lst) / len(lst)

Create a list of numbers greater than the arithmetic mean.

above_ave = [x for x in lst if x > ave]

Compute the sum and the count of the numbers greater than the arithmetic mean.

sum = sum(above_ave)
cnt = len(above_ave)

Print the answer.

print(sum, cnt)