Back to challenges
Easy 0 solved 0% acceptance

Above-Average Values

Keep the values that are greater than the average.

What you'll learn

  • Compute an average first
  • Filter values in a second pass
  • Keep the original order

Problem

Compute the average of the list, then keep only the values strictly greater than it, in their original order.

Implement above_average(numbers).

Input

numbers: The list of integers.

Output

Return the values above the average (an empty list for an empty input).

Constraints

0 ≤ length ≤ 10,000; -1,000,000 ≤ each value ≤ 1,000,000

Examples

Example 1
Input:
numbers = [1, 2, 3, 4, 10]

Expected output:
expected = [10]
Input:
std::vector<long long> numbers = {1, 2, 3, 4, 10};

Expected output:
std::vector<long long> expected = {10};
Input:
long[] numbers = new long[] {1, 2, 3, 4, 10};

Expected output:
long[] expected = new long[] {10};

Explanation

The average is 20 / 5 = 4; only 10 is greater than 4.

Example 2
Input:
numbers = [5, 5, 5]

Expected output:
expected = []
Input:
std::vector<long long> numbers = {5, 5, 5};

Expected output:
std::vector<long long> expected = {};
Input:
long[] numbers = new long[] {5, 5, 5};

Expected output:
long[] expected = new long[] {};

Explanation

Every value equals the average 5, so none is greater: [].

How to approach it

Try on your own first, then open one hint at a time.

Hint 1

First find the list’s average, then keep values strictly above it.

Hint 2

This usually requires one pass to measure the total and another to collect matches.

Hint 3

If the list is empty, the answer should also be empty.

Expected complexity

O(n) time, O(1) extra space

References

This challenge is adapted for CodeUtility. Follow the statement and constraints above.

Learn from others

Community solutions

No published solutions yet. Solve this challenge to share the first one.