Largest and Smallest in Array (Python)

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

πŸ“₯ Input Format
  • Line 1: Integer n (size of array)
  • Line 2: n space-separated integers
πŸ“€ Output Format
  • 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:

START
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 β†’ update max_val.
  • Is this number less than our current min_val? If yes β†’ update min_val.

 

Step 5: Printing the Result

print(“Largest:”, max_val)
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:

Single Element
Both largest and smallest will be the same element. The algorithm handles this correctly.
βž–
All Negatives
e.g. [-5, -10, -3] β€” never initialize to 0 or you’ll get a wrong answer.
Duplicate Values
Algorithm handles duplicates correctly β€” it only updates on strictly greater or lesser values.
πŸ“ˆ
Large Arrays
O(n) is already optimal. No sorting needed β€” sorting would be O(n log n) and slower.

Test Cases

βœ… Test Case 1 β€” Normal
Input
5
3 7 1 9 2
Output
Largest: 9
Smallest: 1
βœ… Test Case 2 β€” Negative Numbers
Input
6
-10 0 5 3 -2 8
Output
Largest: 8
Smallest: -10
βœ… Test Case 3 β€” Single Element
Input
1
42
Output
Largest: 42
Smallest: 42
βœ… Test Case 4 β€” All Same
Input
4
5 5 5 5
Output
Largest: 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.

if you want moreΒ follow
if you more on programmingΒ follow

Leave a Comment