Count Elements Greater Than Average
Count values strictly above a list's average. Students compute the mean first, scan again for matches, and identify empty input as an undefined case here.
1# Take array2n = int(input("Enter array size: "))3arr = []4for i in range(n):5 arr.append(int(input(f"Element {i+1}: ")))67# Calculate average8average = sum(arr) / len(arr)910# Count greater than average11count = 012for element in arr:13 if element > average:14 count += 11516print(f"Elements greater than average ({average}): {count}")
Output
Enter array size: 5 Element 1: 10 Element 2: 20 Element 3: 30 Element 4: 40 Element 5: 50 Elements greater than average (30.0): 2
In this one
- Compute a shared benchmark
- Count strict exceedances
- Recognize zero-length failure
How it works
- 1
Calculate sum(arr) divided by len(arr).
- 2
Scan the entries after the mean is known.
- 3
Increment count only for values strictly above it.
Edge cases
Do values equal to the average count?
Do values equal to the average count?
No. The comparison is strict.
What happens for empty input?
What happens for empty input?
The mean calculation raises ZeroDivisionError.
What's going on
The mean is sum(arr) / len(arr). A second traversal compares each value with that shared benchmark.
Only element > average increases count. Equal-to-average entries do not count, and repeated larger values count separately. Two passes keep the logic simple because the completed mean is needed before classifying entries. An empty list causes division by zero.
Do values equal to the average count? No. The comparison is strict.
What happens for empty input? The mean calculation raises ZeroDivisionError.
Quiz
Tap an option. The answer stays hidden until you pick.
Question 1
Why calculate the mean before counting?
Pick an answer to check it.
Question 2
Do values equal to the average count?
Pick an answer to check it.
Questions people ask
Do values equal to the average count?
No. The comparison is strict.
What happens for empty input?
The mean calculation raises ZeroDivisionError.
Why calculate the mean before counting?
Every comparison needs the completed mean.
๐Related Content
- ๐
Phase 4 - Learn Concepts
Review Phase 4 concepts and explanations
- ๐
Phase 4 - Quiz
Test your Phase 4 understanding with quiz questions
- ๐ป
Phase 4 - All Practice Problems
Explore all practice problems for Phase 4
- ๐
Master Your Logic Building - Complete Course
Browse all phases and tutorials
- ๐ง
Logic Building Overview
Learn about the complete logic building curriculum