Founding Batch Offer ยท HLD ยท LLD ยท System Design at โ‚น799 until 30 Nov

Index of Minimum Element

Find the zero-based index of a list's minimum with Python. Students update a value-index pair and explain why repeated minima keep the first position.

BeginnerPhase 4 - Level 1: Basic ArraysExample 8 of 10
index-of-minimum-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 min
8min_index = 0
9min_value = arr[0]
10
11for i in range(1, len(arr)):
12 if arr[i] < min_value:
13 min_value = arr[i]
14 min_index = i
15
16print(f"Index of minimum: {min_index}")

Output

Enter array size: 5
Element 1: 10
Element 2: 5
Element 3: 20
Element 4: 3
Element 5: 15
Index of minimum: 3

In this one

  • Track minimum positions
  • Retain first tied minima
  • Require nonempty data

How it works

  1. 1

    Initialize the minimum value and index from position zero.

  2. 2

    Inspect each later indexed value.

  3. 3

    Replace both fields only for a smaller candidate.

Edge cases

Which repeated minimum is reported?

Which repeated minimum is reported?

The first one, because equality does not update the pair.

Can an empty list be scanned this way?

Can an empty list be scanned this way?

No. The initial arr[0] access fails.

What's going on

min_index starts at zero and min_value at arr[0]. Each later index supplies a candidate value.

Both variables update only when a strictly smaller value appears. Repeated minima therefore preserve the earliest index. Tracking the index alongside the value avoids a separate lookup pass, but initialization requires at least one element.

Which repeated minimum is reported? The first one, because equality does not update the pair.

Can an empty list be scanned this way? No. The initial arr[0] access fails.

Quiz

Tap an option. The answer stays hidden until you pick.

Question 1

What must update with min_value?

Pick an answer to check it.

Question 2

Which repeated minimum is reported?

Pick an answer to check it.

Questions people ask

Which repeated minimum is reported?

The first one, because equality does not update the pair.

Can an empty list be scanned this way?

No. The initial arr[0] access fails.

What must update with min_value?

min_index.