Largest and smallest in array is a common problem in Python programming interviews.
Problem Statement
Given an array of integers, determine the largest and smallest elements present in the array without using Python’s built-in max() or min() functions.
Input / Output Format
- Line 1: Integer n (size of array)
- Line 2: n space-separated integers
- Largest element
- Smallest element
Flow Chart
Pseudocode
Before writing real Python code, here’s the algorithm in plain pseudocode so the logic is crystal clear:
READ n
READ array of n integersSET max = array[0]
SET min = array[0]FOR each element in array:
IF element > max β max = element
IF element < min β min = elementPRINT max
PRINT min
END
“largest and smallest in array”
Python Program β Find Largest and Smallest in Array
n = int(input())
arr = list(map(int, input().split()))
max_val = arr[0]
min_val = arr[0]
for num in arr:
if num > max_val:
max_val = num
if num < min_val:
min_val = num
print("Largest:", max_val)
print("Smallest:", min_val)
Explanation
Step 1: Taking the Input
n = int(input())
arr = list(map(int, input().split()))
The program reads n β the size of the array. The second line is a Python trick for reading multiple numbers at once. input().split() breaks the input string at every space, map(int, ...) converts each piece to an integer, and list(...) stores them all in a Python list called arr.
Step 2: Smart Initialization
max_val = arr[0]
min_val = arr[0]
β οΈ Common Mistake: Never initialize max_val = 0. If your array contains only negative numbers like [-5, -10, -3], the program would incorrectly return 0 as the largest β which isn’t even in the array!
The correct approach is to initialize both max_val and min_val to arr[0] β the first element. We then compare every remaining element against this starting point.
Step 3: Iterating Through the Array
for num in arr:
A simple for loop visits every element in the list one by one. The variable num takes on the value of each element as the loop progresses.
Step 4: The Comparison Logic
if num > max_val:
max_val = num
if num < min_val:
min_val = num
Inside the loop, two questions are asked for every element:
- Is this number greater than our current
max_val? If yes β updatemax_val. - Is this number less than our current
min_val? If yes β updatemin_val.
Step 5: Printing the Result
print("Smallest:", min_val)
Once the loop finishes checking every element, the final maximum and minimum values are safely stored in max_val and min_val, and printed to the screen.
Time & Space Complexity
| Metric | Value | Reason |
|---|---|---|
| Time Complexity | O(n) | Loop runs once through all n elements |
| Space Complexity | O(1) | No extra memory used β only 2 variables |
π‘ Interview Tip: This O(n) single-pass solution is optimal. You cannot find both max and min in less than nβ1 comparisons. If an interviewer asks “can you do better?”, the answer is no for a general unsorted array.
Edge Cases to Consider When Finding Largest and Smallest in Array
When finding the largest and smallest in array, always handle these special situations:
Test Cases
3 7 1 9 2
Smallest: 1
-10 0 5 3 -2 8
Smallest: -10
42
Smallest: 42
5 5 5 5
Smallest: 5
Conclusion
Finding the largest and smallest in array using Python is one of the most important foundational skills in DSA. The key takeaways are: always initialize to the first element (not zero), use a single O(n) loop, and handle edge cases like all-negative arrays.
Master this pattern and you’ll find it reused in problems like finding the second largest, the k-th smallest, and range queries.