Условие
Return the arithmetic mean of the list: its sum divided by its length, as a decimal number.
Implement array_average(numbers).
Input
numbers: A non-empty list of integers.
Output
Return the average.
Ограничения
1 ≤ length ≤ 100,000; -10⁹ ≤ each value ≤ 10⁹
Примеры
Ввод:
numbers = [2, 4, 6]
Ожидаемый вывод:
expected = 4.0
Ввод:
std::vector<long long> numbers = {2, 4, 6};
Ожидаемый вывод:
double expected = 4.0;
Ввод:
long[] numbers = new long[] {2, 4, 6};
Ожидаемый вывод:
double expected = 4.0;
Пояснение
(2 + 4 + 6) / 3 = 4.
Ввод:
numbers = [1, 2]
Ожидаемый вывод:
expected = 1.5
Ввод:
std::vector<long long> numbers = {1, 2};
Ожидаемый вывод:
double expected = 1.5;
Ввод:
long[] numbers = new long[] {1, 2};
Ожидаемый вывод:
double expected = 1.5;
Пояснение
(1 + 2) / 2 = 1.5; divide as decimals.
Как подступиться
Сначала попробуйте сами, затем открывайте подсказки по одной.
Подсказка 1
The average comes from combining the whole list into one total and then using its length.
Подсказка 2
A single pass can collect the sum, then divide by how many values there are.
Подсказка 3
Be careful to preserve decimal precision instead of using integer division.
Ожидаемая сложность
O(n) time, O(1) extra space
Ссылки
Это задание адаптировано для CodeUtility. Следуйте условию и ограничениям выше.