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

Minimum Element

Read integers and find the minimum with a loop. Students select the first element as an initial candidate and trace smaller replacements through the list.

BeginnerPhase 4 - Level 1: Basic ArraysExample 4 of 10
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 minimum
8min_element = arr[0]
9for element in arr:
10 if element < min_element:
11 min_element = element
12
13print(f"Minimum element: {min_element}")

Output

Enter array size: 5
Element 1: 10
Element 2: 5
Element 3: 20
Element 4: 3
Element 5: 15
Minimum element: 3

In this one

  • Collect a nonempty list
  • Track a smaller candidate
  • Avoid invented sentinels

How it works

  1. 1

    Read and append n integers.

  2. 2

    Initialize min_element from arr[0].

  3. 3

    Replace it on smaller values and print the result.

Edge cases

What happens for n = 0?

What happens for n = 0?

Initialization raises IndexError because the list has no first element.

Do negative values work?

Do negative values work?

Yes. Ordinary numeric comparisons select the smallest.

What's going on

After collecting n integers, min_element is initialized from arr[0]. Every value is compared with the current candidate.

Only a strictly smaller element replaces the minimum. Equal values do not change the result. This works for mixed signs and all-positive lists without an invented sentinel, but requires at least one stored element before indexing the first position.

What happens for n = 0? Initialization raises IndexError because the list has no first element.

Do negative values work? Yes. Ordinary numeric comparisons select the smallest.

Quiz

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

Question 1

Why start from arr[0]?

Pick an answer to check it.

Question 2

What happens for n = 0?

Pick an answer to check it.

Questions people ask

What happens for n = 0?

Initialization raises IndexError because the list has no first element.

Do negative values work?

Yes. Ordinary numeric comparisons select the smallest.

Why start from arr[0]?

It is a real candidate from the data.