Tutte le sfide
Facile 2 risolte 66.7% di accettazione

Above-Average Values

Keep the values that are greater than the average.

Cosa imparerai

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

Problema

Mostra traduzione

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).

Vincoli

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

Esempi

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

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

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

Output atteso:
long[] expected = new long[] {10};

Spiegazione

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

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

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

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

Output atteso:
long[] expected = new long[] {};

Spiegazione

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

Come affrontarlo

Prova prima da solo, poi apri un suggerimento alla volta.

Suggerimento 1

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

Suggerimento 2

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

Suggerimento 3

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

Complessità attesa

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

Riferimenti

Questa sfida è adattata per CodeUtility. Segui la traccia e i vincoli sopra.

La migliore soluzione superata di ogni programmatore. Le più veloci, leggere e meglio valutate stanno in alto.

# Programmatore Linguaggio Tempo Memoria Voto dell'IA Soluzione
1 RCRiccardo Corti Python 1,019 ms - 4/5 Vedi il codice
2 LMLaura Mahlow Python 1,100 ms - 4/5 Vedi il codice

Impara dagli altri

Soluzioni della community

Attenzione spoiler: 2 soluzioni complete sotto Prova prima a risolverla da solo. Puoi comunque sbirciare il codice degli altri sviluppatori e le revisioni dell'IA. Accedi e risolvi la sfida per partecipare alla discussione.