Challenge: Sum Without sum()
Completed
Read n numbers and print their total. Don't use Python's built-in sum() – write the loop yourself. This is an O(n) algorithm.
Input
Line 1: n. Line 2: n whole numbers separated by spaces.
Output
The total.
Example
Input
5 1 2 3 4 5
Output
15
Editor
Python
Hints
Start with total = 0.
Add each number to total inside a for loop.
Solution explanation
One pass over the list, one addition per item: O(n) time and O(1) extra space.
Show solution
n = int(input())
nums = list(map(int, input().split()))
total = 0
for x in nums:
total += x
print(total) 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