Challenge: Above Average
Completed
Read n and then n whole numbers. Print how many of them are strictly greater than the average.
Input
n (1 or more), then n integers.
Output
One integer: the count.
Example
Input
5 1 2 3 4 5
Output
2
Editor
C++
Hints
First loop: read and add up. Then work out the average.
Second loop: count items bigger than the average.
Use a double for the average so 2.5 isn't cut down to 2.
Solution explanation
The average is only known after reading everything, so you need two passes over the stored numbers.
Show solution
#include <iostream>
#include <vector>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> v(n);
double total = 0;
for (int i = 0; i < n; i++) {
cin >> v[i];
total += v[i];
}
double average = total / n;
int count = 0;
for (int x : v) {
if (x > average) count++;
}
cout << count << "\n";
return 0;
} Output
Answer
You can practice without logging in. Log in to save completion.
Save your progress
You're practising as a guest, so your progress won't be saved.
- Track every lab you complete
- Earn verifiable lab certificates
- See your progress in every module