Index of Maximum Element

Find index of maximum element.

BeginnerPhase 4 - Level 1: Basic ArraysExample 7 of 10
index-of-maximum-element.py
1# Take array
2n = int(input("Enter array size: "))
3arr = []
4for i in range(n):
5 arr.append(int(input(f"Element {i+1}: ")))
6
7# Find index of max
8max_index = 0
9max_value = arr[0]
10
11for i in range(1, len(arr)):
12 if arr[i] > max_value:
13 max_value = arr[i]
14 max_index = i
15
16print(f"Index of maximum: {max_index}")

Output

Enter array size: 5
Element 1: 10
Element 2: 25
Element 3: 5
Element 4: 30
Element 5: 15
Index of maximum: 3

What's going on

Track both value and index.

Key Concepts:

Initialize with first element
Track index when updating max
Print index of maximum