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

Range (Max - Min)

Calculate a list's numerical range as maximum minus minimum. Students guard empty input and distinguish this statistical range from the range iterator.

AdvancedPhase 4 - Level 5: Logical & AppliedExample 5 of 14
range-max-min.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# Calculate range
8if len(arr) > 0:
9 max_val = max(arr)
10 min_val = min(arr)
11 range_val = max_val - min_val
12 print(f"Range: {range_val}")
13else:
14 print("Array is empty")

Output

Enter array size: 5
Element 1: 10
Element 2: 5
Element 3: 20
Element 4: 3
Element 5: 15
Range: 17

In this one

  • Measure numerical spread
  • Guard empty collections
  • Distinguish range meanings

How it works

  1. 1

    Check arr is nonempty.

  2. 2

    Find maximum and minimum.

  3. 3

    Subtract minimum from maximum and print the spread.

Edge cases

What if all entries are equal?

What if all entries are equal?

The range is zero.

What happens for an empty list?

What happens for an empty list?

Array is empty is printed.

What's going on

The nonempty branch finds max(arr) and min(arr). Their difference measures the spread between the two extreme values.

For [2, 9, 5], the range is seven. This result is a number, unrelated to creating a Python range object. Equal values produce zero spread. Empty input receives a message instead of calling max or min without data.

What if all entries are equal? The range is zero.

What happens for an empty list? Array is empty is printed.

Quiz

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

Question 1

Which calculation measures this range?

Pick an answer to check it.

Question 2

What if all entries are equal?

Pick an answer to check it.

Questions people ask

What if all entries are equal?

The range is zero.

What happens for an empty list?

Array is empty is printed.

Which calculation measures this range?

max_val - min_val.