Challenge: Biggest Number
Completed
Read n numbers and print the largest one and its position (index, starting at 0). If the largest appears more than once, give the first position. Don't use max().
Input
Line 1: n (n ≥ 1). Line 2: n whole numbers.
Output
<largest> at index <i>
Example
Input
5 3 9 2 9 1
Output
9 at index 1
Editor
Python
Hints
Remember the index of the best value so far, starting at 0.
Use > (not >=) so the first position wins when there's a tie.
Solution explanation
We look at each item once, so this is O(n).
Show solution
n = int(input())
nums = list(map(int, input().split()))
best = 0
for i in range(1, n):
if nums[i] > nums[best]:
best = i
print(nums[best], "at index", best) 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