Sum Except Max & Min
Calculate sum of array excluding max and min.
AdvancedPhase 4 - Level 5: Logical & AppliedExample 6 of 14
sum-except-max-min.py
1# Take array2n = int(input("Enter array size: "))3arr = []4for i in range(n):5 arr.append(int(input(f"Element {i+1}: ")))67# Sum except max and min8if len(arr) <= 2:9 print("Sum: 0 (array too small)")10else:11 max_val = max(arr)12 min_val = min(arr)13 total_sum = sum(arr)14 result = total_sum - max_val - min_val15 print(f"Sum (excluding max and min): {result}")
Output
Enter array size: 5 Element 1: 10 Element 2: 5 Element 3: 20 Element 4: 3 Element 5: 15 Sum (excluding max and min): 30
What's going on
Subtract max and min from total sum.
Key Concepts:
Calculate total sum
Subtract max and min
Result is sum of remaining
🔗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