समस्या
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 के लिए अनुकूलित की गई है। ऊपर दिए गए स्टेटमेंट और सीमाओं का पालन करें।